Python 安装 Virtualenv 并激活 virtualenv 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39934906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installed Virtualenv and activating virtualenv doesn't work
提问by abhi_bond
I cloned my Django Project from Github Account and activated the virtualenv using famous command source nameofenv/bin/activateAnd when I run python manage.py runserver
我从 Github 帐户克隆了我的 Django 项目并使用著名的命令激活了 virtualenvsource nameofenv/bin/activate当我运行时python manage.py runserver
It gives me an error saying:
它给了我一个错误说:
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
导入错误:无法导入 Django。您确定它已安装并在您的 PYTHONPATH 环境变量中可用吗?您是否忘记激活虚拟环境?
回答by Jér?me
I was thinking that every and each dependency I need, might be present inside virtualenv.
我在想我需要的每个依赖项都可能存在于 virtualenv 中。
Well, no. By default, a newly created virtualenv comes empty, that is, with no third-party library. (Optionaly, you may allow a virtualenv to access libraries installed system-wide, but that's another story.)
嗯,不。默认情况下,新创建的 virtualenv 是空的,即没有第三方库。(或者,您可以允许 virtualenv 访问系统范围内安装的库,但那是另一回事。)
Once the virtualenv is created, you need to install the dependencies you need.
创建 virtualenv 后,您需要安装所需的依赖项。
(How could virtualenv know what dependencies you need?)
(virtualenv 怎么知道你需要什么依赖?)
The procedure is to install the virtualenv, activate it, and then install the libraries needed for the project (in you case Django and perhaps others).
该过程是安装 virtualenv,激活它,然后安装项目所需的库(在您的情况下是 Django 和其他库)。
If you project has a requirements.txt, you may install every required dependency with the command:
如果您的项目有一个 requirements.txt,您可以使用以下命令安装所有必需的依赖项:
pip install -r requirements.txt
If your project has a setup.py, you may also execute
如果你的项目有setup.py,你也可以执行
pip install -e path/to/your/project/clone/.
to install the project in the virtualenv. This should install the dependencies.
在 virtualenv 中安装项目。这应该安装依赖项。
Of course, if the only dependency is Django, you can just type
当然,如果唯一的依赖是Django,你可以直接输入
pip install django
回答by Dimitris Kougioumtzis
on ubuntu version
在 ubuntu 版本上
#install python pip
sudo apt-get install python-pip
#install python virtualenv
sudo apt-get install python-virtualenv
# create virtual env
virtualenv myenv
#activate the virtualenv
. myenv/bin/activate
#install django inside virtualenv
pip install django
#create a new django project
django-admin.py startproject mysite
#enter to the folder of the new django project
cd mysite
#run the django project
python manage.py runserver
回答by slackmart
I'm guessing you also upload the virtual environment from your other pc. And you hope that only activating that will work, bzz.
我猜你也从你的另一台电脑上传了虚拟环境。你希望只有激活才能奏效,bzz。
It's not recommended to upload the virtualenv files to your git repository, as @Alain says it's a good practice to have a requirements.txtfile containing the project dependencies. You can use pip freeze > requirements.txt(when the environment is activated) to generate the project requirements file.
不建议将 virtualenv 文件上传到您的 git 存储库,因为@Alain 说拥有requirements.txt包含项目依赖项的文件是一个好习惯。您可以使用pip freeze > requirements.txt(当环境激活时)生成项目需求文件。
By doing so, when you clone the repository from another computer, you need to create a new virtualenv by issuing the command:
通过这样做,当您从另一台计算机克隆存储库时,您需要通过发出以下命令来创建一个新的 virtualenv:
virtualenv nameofenv
then activate it:
然后激活它:
source nameofenv/bin/activate
and finally, use the requirements file to install the requirements for your project using:
最后,使用需求文件为您的项目安装需求:
pip install -r requirements.txt
回答by W.Perrin
If you have several pythonon your machine, for example,python2.7, python3.4, python3.6, it is import to figure out which version the pythonreally reference to, and more over, which version does pipreference to.
如果您python的机器上有多个,例如,python2.7, python3.4, python3.6,则需要找出python真正引用的版本,更重要的是确定pip引用的版本。
The same problem got in my way after I installed the let's encryptwhen I run the following command.
let's encrypt当我运行以下命令时,安装后遇到了同样的问题。
(python3 manage.py runserver 0:8000 &)
I inspected the pythonversion and found that python3, python3.4, python3.6, python3.4mwere all available.
我检查了python版本和发现python3,python3.4,python3.6,python3.4m都是可用的。
I just change python3to python3.6and solved the problem.
我只是改变python3到python3.6和解决的问题。
(python3.6 manage.py runserver 0:8000 &)
So, this is probably a version mismatching problem if it is OK for a long time and crashes down suddenly.
所以,如果长时间正常,突然崩溃,这可能是版本不匹配的问题。
回答by cph2117
I had installed Django 2 via pip3 install Django, but I was running python manage.py runserverinstead of python3 manage.py runserver. Django 2 only works with python 3+.
我已经通过 安装了 Django 2 pip3 install Django,但我正在运行python manage.py runserver而不是python3 manage.py runserver. Django 2 仅适用于 python 3+。

