Virtualenv 在 Windows 上不断加载全局站点包

发布于 2024-09-30 17:36:13 字数 1948 浏览 7 评论 0原文

我环顾四周,发现我的问题的答案还没有让我解决它。

我想使用隔离的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄兵必败 2024-10-07 17:36:13

一些额外的 .bat hackery 可以轻松解决这个问题。我对 activate.bat 的标准添加是:

REM custom venv settings
set PYTHONPATH=%\VIRTUAL_ENV%;%\VIRTUAL_ENV%\conf;%\VIRTUAL_ENV%\apps
set DJANGO_SETTINGS_MODULE=settings

ftype Python.File=%VIRTUAL_ENV%\Scripts\python.exe %1 %*

和 deactivate.bat

REM restore ftype
ftype Python.File=C:\tools\Python27\python.exe %1 %*

A little bit of extra .bat hackery can easily fix this. My standard additions to activate.bat are:

REM custom venv settings
set PYTHONPATH=%\VIRTUAL_ENV%;%\VIRTUAL_ENV%\conf;%\VIRTUAL_ENV%\apps
set DJANGO_SETTINGS_MODULE=settings

ftype Python.File=%VIRTUAL_ENV%\Scripts\python.exe %1 %*

and to deactivate.bat

REM restore ftype
ftype Python.File=C:\tools\Python27\python.exe %1 %*
独守阴晴ぅ圆缺 2024-10-07 17:36:13

您可以创建一个 .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.

池予 2024-10-07 17:36:13

我在使用 cmd.exe 的 Windows 7 设置中遇到了与 Dan 相同的“拒绝访问”问题,并使用 m0nonoke 的答案。

但我发现这个工作可以使用替换 shell TCC/LE 和自定义启动文件。 ..

在工作目录下创建子目录config。在这个目录下
为 TCC/LE 创建名为 tcstart.btm 的启动文件

<前><代码>@echo 关闭
rem 覆盖系统 python 绑定来处理虚拟环境
设置 .py;.pyc=python.exe

现在在桌面上创建(复制)TCC/LE 快捷方式并重命名
适当地。打开快捷方式的属性并添加到目标
“C:\django\config\tcstart.btm”。您可能希望将“开始”设置为
一些有用的东西,比如 C:\django

在本指南中找到的有关安装 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...

Under working directory create subdirectory config. In this directory
create startup file for TCC/LE called tcstart.btm

@echo off
rem Override system python binding to handle virtualenvironments
set .py;.pyc=python.exe

Now create (copy) TCC/LE shortcut on desktop and rename it
appropriately. Open Properties for shortcut and add to Target
“C:\django\config\tcstart.btm”. You probably want to set Start in to
something useful, like C:\django

Solution found in this guide on installing Django and Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文