简体   繁体   English

如何在Windows的virtualenv中设置Django?

[英]How to setup Django inside virtualenv on Windows?

Any Windows python developers here? 这里有Windows python开发人员吗?

I created a virtualenv, activated it and installed django . 我创建了一个virtualenv,将其激活并安装了django If I run pip3 list , it shows Django (2.0.3) as installed. 如果我run pip3 list ,它将显示已安装的Django(2.0.3)。 The problem is when I try and use django, it never works, it just returns "no module django." 问题是,当我尝试使用django时,它永远不会起作用,它只会返回“ no module django”。

When I try pip3 install django again, it says it's already installed at myname\\envs...\\site-packages. 当我再次尝试pip3 install django时,它说它已经安装在myname \\ envs ... \\ site-packages中。 But when I use the django command it never looks at this path, it looks at appdata/local/programs/python/python36-32/python.exe (ie not the virtualenv but the python installation itself). 但是,当我使用django命令时,它永远不会查看此路径,而是会查看appdata / local / programs / python / python36-32 / python.exe(即不是virtualenv而是python安装本身)。

Anyone have any ideas? 有人有想法么?

Maybe you have tried to use pip3 which located in your real python environment. 也许您已尝试使用位于实际python环境中的pip3。 But actually your django has been installed in virtual environment. 但是实际上您的django已安装在虚拟环境中。 From your example, maybe you should check the python interpreter something like this: venv/bin/python3.4. 从您的示例中,也许您应该检查python解释器,例如:venv / bin / python3.4。

By the way, before you use django-admin command, you should make sure you have already in virtual env. 顺便说一句,在使用django-admin命令之前,应确保已在虚拟环境中。

For example, if you are in virtual env, it will be someting like this: 例如,如果您在虚拟环境中,它将像这样:

(venv)Mac:myproject michael$ pip install jinja2
...
Successfully installed jinja2-2.7.3 markupsafe-0.23
(venv)Mac:myproject michael$ python myapp.py
...

If you want to get into the real python environment: 如果您想进入真正的python环境:

(venv)Mac:myproject michael$ deactivate 
Mac:myproject michael$

So, you should make sure you have been in the same environment with both packages and interpreter. 因此,您应该确保使用软件包和解释器都处于同一环境中。

在激活virtualenv python环境后,应使用pip

Refer to this section of the documentation https://docs.djangoproject.com/en/2.0/intro/contributing/#getting-a-copy-of-django-s-development-version 请参阅文档的本部分https://docs.djangoproject.com/en/2.0/intro/contributing/#getting-a-copy-of-django-s-development-version

For Windows users Using the built-in venv module will not work if you are also using the Git Bash shell on Windows, since activation scripts are only created for the system shell (.bat) and PowerShell (.ps1). 对于Windows用户,如果您还在Windows上也使用Git Bash shell,则无法使用内置venv模块,因为仅针对系统shell(.bat)和PowerShell(.ps1)创建了激活脚本。 Use the virtualenv package instead: 请使用virtualenv软件包:

$ pip install virtualenv
$ virtualenv ~/.virtualenvs/djangodev

Then you start using your virtual environment with 然后,您可以通过以下方式使用虚拟环境:

workon <<your_virtual_env_name>>

Then every package you install will be within this environment 然后,您安装的每个软件包都将在此环境中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM