Virtualenv 在 Windows 上不断加载全局站点包
我环顾四周,发现我的问题的答案还没有让我解决它。
我想使用隔离的 virtualenv 环境,但出于某种原因,当在 django 的 shell 中时,virtualenv 不断加载全局站点包...
我尝试清理 PATH 变量,直到只有 c:\Python26\Scripts 和 c:\Python26保持。然后我创建我的环境。
virtualenv --distribute --no-site-packages myproject
然后我激活 virtualenv。 PATH 现在(不相关的变量已废弃):
PATH=E:\Development\django_projects\myproject\Scripts;C:\Panda3D-1.7.0\python;C:\Panda3D-1.7.0\bin;c:\python26\Scripts;
PYTHONPATH=C:\Panda3D-1.7.0\
到目前为止,一切顺利。我启动 python...
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
让我们尝试一个我确信位于我的 c:\python site-packages 目录中的模块。
>>> import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named BeautifulSoup
耶!没有全球站点包!然后继续下一篇。在命令提示符下,我输入:
django-admin.py
它有效了!但是等等...我还没有安装 Django。这怎么可能?
在此之后,它变得更加奇怪......我首先将这些添加到 virtualenv 的 activate.bat 脚本中,以便 Django 可以找到我的设置。
set PYTHONPATH=E:\Development\django_projects\myproject\
set DJANGO_SETTINGS_MODULE=settings.development
现在我启动 django-admin.py shell,
In [1]: import BeautifulSoup
In [2]: BeautifulSoup.__file__
Out[2]: 'C:\\Python26\\lib\\site-packages\\BeautifulSoup.pyc'
这怎么可能?
灵光乍现
在打字的时候,我突然明白了。 .py 是与我的 c:\python26\python.exe 可执行文件(而不是 virtualenv 可执行文件)相结合的文件扩展名!
python manage.py
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management
呵呵。任何人都知道如何将 .py 文件扩展名耦合到我的 virtualenv 的 python 可执行文件而不是系统定义的 python 可执行文件?
I've looked around on SO, and the answers I have found to my problem haven't allowed me to solve it yet.
I want to use isolated virtualenv environments, but for one reason or another, virtualenv keeps loading global site packages, when in django's shell...
I tried to clean up PATH variables, until only c:\Python26\Scripts and c:\Python26 remain. I then create my environment.
virtualenv --distribute --no-site-packages myproject
I then activate the virtualenv. PATH is now (irrelevant vars scrapped):
PATH=E:\Development\django_projects\myproject\Scripts;C:\Panda3D-1.7.0\python;C:\Panda3D-1.7.0\bin;c:\python26\Scripts;
PYTHONPATH=C:\Panda3D-1.7.0\
So far, so good. I launch python...
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
Let's just try a module I'm sure is in my c:\python site-packages directory.
>>> import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named BeautifulSoup
Yay! No global site packages! On to the next one then. From the command prompt, I type:
django-admin.py
And it works! But wait... I haven't installed Django yet. How is this possible?
After this, it gets even weirder... I first add these to virtualenv's activate.bat script so that Django can find my settings.
set PYTHONPATH=E:\Development\django_projects\myproject\
set DJANGO_SETTINGS_MODULE=settings.development
Now I launch django-admin.py shell and
In [1]: import BeautifulSoup
In [2]: BeautifulSoup.__file__
Out[2]: 'C:\\Python26\\lib\\site-packages\\BeautifulSoup.pyc'
How is this even possible?
Flash of insight
While typing this, I suddenly get it. .py is a file extension coupled with my c:\python26\python.exe executable, instead of the virtualenv one!
python manage.py
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management
Heh. Anyone has any idea of how to couple the .py file extension to my virtualenv's python executable instead of the system defined python executable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些额外的 .bat hackery 可以轻松解决这个问题。我对 activate.bat 的标准添加是:
和 deactivate.bat
A little bit of extra .bat hackery can easily fix this. My standard additions to activate.bat are:
and to deactivate.bat
您可以创建一个 .bat 文件并修改其中的 PATH 和 PYTHONPATH,然后从该 .bat 文件运行 .py。
我认为类似这样
set PATH=C:\Python26;
python myfile.py
当然,可以将其他任何内容添加到您想要的路径中。
You could make a .bat file and modify PATH and PYTHONPATH in there, and then run .py from that .bat file.
Something like this i think
set PATH=C:\Python26;
python myfile.py
Ofcourse, add anything else to your path that you want.
我在使用 cmd.exe 的 Windows 7 设置中遇到了与 Dan 相同的“拒绝访问”问题,并使用 m0nonoke 的答案。
但我发现这个工作可以使用替换 shell TCC/LE 和自定义启动文件。 ..
在本指南中找到的有关安装 Django 的解决方案和Windows。
I had the same "Access denied" problems as Dan with m0nonoke's answer on my Windows 7 setup using cmd.exe.
But I found this work around using a replacement shell TCC/LE and a customised startup file...
Solution found in this guide on installing Django and Windows.