简体   繁体   English

Windows上的django-admin.py和virtualenv问题

[英]django-admin.py and virtualenv issue on Windows

In my system there is Django 1.2.3 installed system wide: 在我的系统中,系统安装了Django 1.2.3:

C:\>python -c "import django; print django.get_version()"
1.2.3
C:\>django-admin.py --version
1.2.3

Then there is a virtual environment called venv in C:\\dev where I installed Django 1.2.4: 然后在C:\\ dev中有一个名为venv的虚拟环境,我安装了Django 1.2.4:

C:\> dev\venv\Scripts\activate.bat
(venv) C:\> python -c "import django; print django.get_version()"
1.2.4
(venv) C:\> django-admin.py --version
1.2.3

My questions: 我的问题:

  1. Why django-admin.py reports version 1.2.3, if the current Python (virtual) environment has django 1.2.4 installed? 如果当前的Python(虚拟)环境安装了django 1.2.4,为什么django-admin.py会报告版本1.2.3?
  2. How can I use Django's 1.2.4 django-admin.py automatically when venv is active? venv处于活动状态时,如何自动使用Django的1.2.4 django-admin.py?

Additional info: 附加信息:

  • virtualenv version: 1.5.1, Python version 2.7 virtualenv版本:1.5.1,Python版本2.7
  • command used to create venv : C:\\dev\\> virtualenv --no-site-packages venv 用于创建venv的命令: C:\\dev\\> virtualenv --no-site-packages venv
  • (venv) C:\\> echo %PATH%

    C:\\dev\\venv\\Scripts; ...other paths...

  • shebang of django-admin.py in venv : #!C:\\dev\\Scripts\\python.exe 在venv中的django-admin.py的 shebang: #!C:\\dev\\Scripts\\python.exe

Hope you can help, many thanks. 希望你能帮忙,非常感谢。

This is because your windows has associated .py extension with the globally installed python.exe . 这是因为您的Windows与全局安装的python.exe具有关联的.py扩展名。 Therefore when you type django-admin.py , even though you're in a virtualenv, the global python is invoked, and it in turn finds your global django installation in its own site-packages. 因此,当您键入django-admin.py ,即使您处于virtualenv中,也会调用全局python,然后它会在自己的站点包中找到您的全局django安装。 Try python django-admin.py to circumvent the association. 尝试使用python django-admin.py来规避关联。

As shanyu already explained, it is because of *.py file associations made to your Python install executable instead of your virtualenv. 正如shanyu已经解释的那样,这是因为* .py文件关联到你的Python安装可执行文件而不是你的virtualenv。 However, to answer your second question differently, I solved this problem by creating a django-admin.bat in my virtualenv's Scripts directory. 但是,为了不同地回答你的第二个问题,我通过在virtualenv的Scripts目录中创建一个django-admin.bat解决了这个问题。 Its contents? 它的内容?

@echo off
python %VIRTUAL_ENV%\Scripts\django-admin.py %*

Now you can use django-admin startproject <project_name> . 现在您可以使用django-admin startproject <project_name> The necessary PATH and VIRTUAL_ENV environment variables should have already been set correctly by virtualenv when you activated the environment. 激活环境时,virtualenv应该已经正确设置了必要的PATHVIRTUAL_ENV环境变量。

我只是键入了django-admin ,没有.py文件扩展名,并为我工作。

I had to point the "global python.exe" to my virtualenv in my project so I created my own activate.cmd 我不得不在我的项目中将“global python.exe”指向我的virtualenv,所以我创建了自己的activate.cmd

set THE_PATH=c:\my-envs\my-specific-env\Scripts
ftype Python.File="%THE_PATH%\python.exe" %%1 %%*
%THE_PATH%\activate.bat

It changes the the file type association using windows command 'ftype'. 它使用windows命令'ftype'更改文件类型关联。

I had a similar problem on linux when I tried to use an already exisiting django project with a later installed virtualenv. 当我尝试使用已经 安装的 virtualenv 已经存在的django项目时,我在linux上遇到了类似的问题。

Is it possible that django-admin.py of django 1.2.4 is not on your path but that django-admin.py of your django 1.2.3 install is? 是否有可能django 1.2.4的django-admin.py 不在你的路径上,但你的django 1.2.3安装的django-admin.py是?

That would explain your output from 这可以解释你的输出

C:\> dev\venv\Scripts\activate.bat
(venv) C:\> python -c "import django; print django.get_version()"
1.2.4
(venv) C:\> django-admin.py --version
1.2.3

because the python command is on the path of your virtualenv but the django-admin.py file might not be. 因为python命令位于virtualenv的路径上,但django-admin.py文件可能不是。

As to your second question (assuming my guess above is correct): sym-link the django-admin.py file into your C:\\dev\\venv\\Scripts directory, although I am not sure how that works on windows (are you using Cygwin?). 至于你的第二个问题(假设我的猜测是正确的):sym-link django-admin.py文件到你的C:\\dev\\venv\\Scripts目录,虽然我不确定它在windows上是如何工作的(你使用的是什么) Cygwin的?)。

Of course you can always call it as python C:\\path\\to\\django-admin.py (since the right python version is called) but of course that is a lot of typing. 当然你总是把它称为python C:\\path\\to\\django-admin.py (因为调用了正确的python版本),但当然这是很多打字。

i used Philip Nelson's solution but had to add quotes for spaces in my filename: 我使用了Philip Nelson的解决方案但是必须在我的文件名中添加空格的引号:

python "%VIRTUAL_ENV%\\Scripts\\django-admin.py" %* python“%VIRTUAL_ENV%\\ Scripts \\ django-admin.py”%*

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

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