在 Windows 上使用 fabfile 中的 activate_this.py 激活 python 虚拟环境
我有一个 Fabric 任务需要访问我的 Django 项目的设置。
在 Windows 上,我无法将 Fabric 安装到项目的 virtualenv 中(Paramiko + pycrypto deps 的问题)。但是,我可以在系统范围的站点包中安装 Fabric,没有问题。
我已将 Django 安装到项目的 virtualenv 中,当我使用“VIRTUALENV\Scripts\activate.bat”脚本激活 virtualenv 时,我可以轻松使用所有“> python manage.py”命令。
我的项目中有一个结构任务文件(fabfile.py),它提供了设置、测试、部署等任务。我的 fabfile 中的一些任务需要通过“from django.conf import settings”访问我的 django 项目的设置”。
由于我唯一可用的 Fabric 安装位于系统范围的站点包中,因此我需要在 fabfile 中激活 virtualenv,以便 django 可用。为此,我使用项目 virtualenv 的“activate_this”模块来访问项目设置等。在执行 activate_this.py 之前和之后使用“print sys.path”,我可以告诉 python 路径更改指向项目的 virtualenv。但是,我仍然无法导入 django.conf.settings。
我已经能够在 *nix(Ubuntu 和 CentOS)和 Cygwin 上成功完成此操作。您在 Windows 上使用此设置/工作流程吗?如果是这样,您能帮我弄清楚为什么这在 Windows 上不起作用,或者提供任何提示和技巧来解决此问题吗?
谢谢和干杯。
参考:
- http://virtualenv.openplans.org/#id9 |使用 Virtualenv 而不使用 bin/python
本地开发环境:
- Python 2.5.4
- Virtualenv 1.4.6
- Fabric 0.9.0
- Pip 0.6.1
- Django 1.1.1
- Windows XP (SP3)
I have a Fabric task that needs to access the settings of my Django project.
On Windows, I'm unable to install Fabric into the project's virtualenv (issues with Paramiko + pycrypto deps). However, I am able to install Fabric in my system-wide site-packages, no problem.
I have installed Django into the project's virtualenv and I am able to use all the "> python manage.py" commands easily when I activate the virtualenv with the "VIRTUALENV\Scripts\activate.bat" script.
I have a fabric tasks file (fabfile.py) in my project that provides tasks for setup, test, deploy, etc. Some of the tasks in my fabfile need to access the settings of my django project through "from django.conf import settings".
Since the only usable Fabric install I have is in my system-wide site-packages, I need to activate the virtualenv within my fabfile so django becomes available. To do this, I use the "activate_this" module of the project's virtualenv in order to have access to the project settings and such. Using "print sys.path" before and after I execute activate_this.py, I can tell the python path changes to point to the virtualenv for the project. However, I still cannot import django.conf.settings.
I have been able to successfully do this on *nix (Ubuntu and CentOS) and in Cygwin. Do you use this setup/workflow on Windows? If so Can you help me figure out why this wont work on Windows or provide any tips and tricks to get around this issue?
Thanks and Cheers.
REF:
- http://virtualenv.openplans.org/#id9 | Using Virtualenv without
bin/python
Local development environment:
- Python 2.5.4
- Virtualenv 1.4.6
- Fabric 0.9.0
- Pip 0.6.1
- Django 1.1.1
- Windows XP (SP3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一番挖掘,我发现这是 activate_this.py 脚本的问题。在当前状态下,virtualenv<=1.4.6,此脚本假设 site-packages 目录的路径对于所有平台都是相同的。但是,*nix 类平台和 Windows 之间的 site-packages 目录路径有所不同。
在这种情况下,activate_this.py 脚本将 *nix 样式路径:
VIRTUALENV_BASE/lib/python2.5/site-packages/
添加到 python 路径,而不是 Windows 特定路径:
VIRTUALENV_BASE\Lib\site-packages\
我创建了一个virtualenv 问题跟踪器中的问题概述了问题和解决方案。如果您有兴趣,可以在这里查看该问题: http://bitbucket.org/ianb/virtualenv/issue/31/windows-activate_this-assumes-nix-path-to-site
希望修复程序将在即将发布的 virtualenv 中提供。
如果您现在需要修复此问题,并且 virtualenv 软件包尚未修补,您可以“修复”您自己的 activate_this.py,如下所示。
编辑 VIRTUALENV\Scripts\activate_this.py 文件。将行 (17 ?): 更改
为
完成此操作后,您的 activate_this.py 脚本将首先检查它正在运行的平台,然后定制 site-packages 目录的路径以适应。
享受!
After some digging, I found out that this is an issue with the activate_this.py script. In it's current state, virtualenv<=1.4.6, this script assumes that the path to the site-packages directory is the same for all platforms. However, the path to the site-packages directory differs between *nix like platforms and Windows.
In this case the activate_this.py script adds the *nix style path:
VIRTUALENV_BASE/lib/python2.5/site-packages/
to the python path instead of the Windows specific path:
VIRTUALENV_BASE\Lib\site-packages\
I have created an issue in the virtualenv issue tracker which outlines the problem and the solution. If you are interested, you may check on the issue here: http://bitbucket.org/ianb/virtualenv/issue/31/windows-activate_this-assumes-nix-path-to-site
Hopefully the fix will be made available in an upcomming release of virtualenv.
If you need a fix for this problem right now, and the virtualenv package has not yet been patched, you may "fix" your own activate_this.py as shown below.
Edit your VIRTUALENV\Scripts\activate_this.py file. Change the line (17 ?):
to
With this in place, your activate_this.py script would first check which platform it is running on and then tailor the path to the site-packages directory to fit.
Enjoy!
您必须从 fab 文件中执行激活此操作。虽然我还没有测试过,但我相信以下应该有效:
You will have to execute the activate this, from within the fab file. Altho' I have not tested it, I believe following should work: