在 Django 中设置虚拟环境

问题描述

我刚开始学习 django,对如何设置虚拟环境很困惑。

我已经成功安装了python:

当我运行 import java.util.ArrayList; import java.util.*; class Node { public Node parent; public int[][] board; // Blank tile cordinates public int gx,gy; // Number of misplaced tiles public int cost; // The number of moves so far public int level; public Node(int[][] arr,int x,int y,int nX,int nY,int lev,Node par) { parent = par; board = new int[arr.length][]; for (int i = 0; i < arr.length; i++) { board[i] = arr[i].clone(); } board[x][y] = board[nX][nY]; board[nX][nY] = 0; cost = Integer.MAX_VALUE; level = lev; gx = nX; gy = nY; } } public class PuzzleProblem { public static int limit = 3; // Bottom,left,top,right static int[] row = { 1,-1,0 }; static int[] col = { 0,1 }; public static int calculateCost(int[][] initial,int[][] goal) { int count = 0; int n = initial.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (initial[i][j] != 0 && initial[i][j] != goal[i][j]) { count++; } } } return count; } public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } public static boolean isSafe(int x,int y) { return (x >= 0 && x < limit && y >= 0 && y < limit); } public static void printPath(Node root) { if (root == null) { return; } printPath(root.parent); printMatrix(root.board); System.out.println(); } public static void solve(int[][] initial,int[][] goal,int y) { double T = 10; double cool = 0.9; double Tmin = .0001; Node oldstate = new Node(initial,x,y,null); oldstate.cost = calculateCost(initial,goal); Node newstate; while (T > Tmin) { /*Node min = oldstate; if (min.cost == 0) { printPath(oldstate); return; }*/ for (int i = 0; i < 4; i++) { if (isSafe(oldstate.gx + row[i],oldstate.gy + col[i])) { newstate = new Node(oldstate.board,oldstate.gx,oldstate.gy,oldstate.gx + row[i],oldstate.gy + col[i],oldstate.level + 1,oldstate); newstate.cost = calculateCost(newstate.board,goal); double ch= Math.exp((oldstate.cost- newstate.cost)/T); System.out.println(oldstate.cost);System.out.println(newstate.cost); if (newstate.cost < oldstate.cost) { newstate = oldstate; } else if (ch > Math.random()) { System.out.println(ch ); newstate = oldstate; } printPath(newstate); } } T*=cool; } } public static void main(String[] args) { int[][] initial = { {4,3},{7,1,2},{5,8,6} }; int[][] goal = { {1,2,{8,4},6,5} }; // White tile coordinate int x = 0,y = 1; solve(initial,goal,y); } } 时,我得到 python --version

当我运行 Python 3.8.1 时,我得到 python3 --version

我的第一个问题是当我运行 Python 3.8.1 时我得到这个

which -a python3

/Library/Frameworks/Python.framework/Versions/3.8/bin/python3

/usr/local/bin/python3

有人能帮我理解为什么我有 3 个地方存在 python3 吗?

我的第二个问题是我已经成功安装了 /usr/bin/python3virtaulenv

当我运行 virtualenvwrapper 时,我得到:

virtualenv --version

当我运行 virtualenv 20.4.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/virtualenv/__init__.py 时,我得到:

virtualenvwrapper --version

这就是为什么当我运行 -bash: virtualenvwrapper: command not found 时,我一直找不到命令。

我的第三个问题是 mkvirtualenv testvirtualenv test 之间有什么区别?

解决方法

如果您使用的是 Ubuntu,则可以按照以下步骤操作

  1. 转到要保存 Django 项目的文件夹
  2. 运行命令 python -m venv env
  3. env 是虚拟环境的名称,如果 python 不起作用,请尝试使用 python3
  4. activate environment by source env/bin/activate env 是环境的名称。
  5. 安装所有依赖项,创建一个新文件夹并在其中导入 Django 项目。