简体   繁体   English

在Windows上的fabfile中使用activate_this.py激活python虚拟环境

[英]Activate a python virtual environment using activate_this.py in a fabfile on Windows

I have a Fabric task that needs to access the settings of my Django project. 我有一个Fabric任务需要访问我的Django项目的设置。

On Windows, I'm unable to install Fabric into the project's virtualenv (issues with Paramiko + pycrypto deps). 在Windows上,我无法将Fabric安装到项目的virtualenv中(Paramiko + pycrypto deps的问题)。 However, I am able to install Fabric in my system-wide site-packages, no problem. 但是,我能够在我的系统范围的站点包中安装Fabric,没问题。

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. 我已经将Django安装到项目的virtualenv中,当我使用“VIRTUALENV \\ Scripts \\ activate.bat”脚本激活virtualenv时,我可以轻松地使用所有“> python manage.py”命令。

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". 我的项目中有一个结构任务文件(fabfile.py),它提供设置,测试,部署等任务。我的fabfile中的一些任务需要通过“来自django.conf导入设置来访问我的django项目的设置”。

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. 由于我唯一可用的Fabric安装在我的系统范围的站点包中,我需要激活fabfile中的virtualenv,以便django可用。 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. 为此,我使用项目virtualenv的“activate_this”模块来访问项目设置等。 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. 在执行activate_this.py之前和之后使用“print sys.path”,我可以告诉python路径更改指向项目的virtualenv。 However, I still cannot import django.conf.settings. 但是,我仍然无法导入django.conf.settings。

I have been able to successfully do this on *nix (Ubuntu and CentOS) and in Cygwin. 我已经能够在* nix(Ubuntu和CentOS)和Cygwin上成功完成这项工作。 Do you use this setup/workflow on Windows? 您是否在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? 如果是这样你能帮我弄清楚为什么它不适用于Windows或提供任何提示和技巧来解决这个问题?

Thanks and Cheers. 谢谢和干杯。


REF: REF:

Local development environment: 本地开发环境:

  • Python 2.5.4 Python 2.5.4
  • Virtualenv 1.4.6 Virtualenv 1.4.6
  • Fabric 0.9.0 面料0.9.0
  • Pip 0.6.1 点0.6.1
  • Django 1.1.1 Django 1.1.1
  • Windows XP (SP3) Windows XP(SP3)

After some digging, I found out that this is an issue with the activate_this.py script. 经过一番挖掘后,我发现这是activate_this.py脚本的一个问题。 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. 在当前状态virtualenv <= 1.4.6中,此脚本假定site-packages目录的路径对于所有平台都是相同的。 However, the path to the site-packages directory differs between *nix like platforms and Windows. 但是, site-packages目录的路径在* nix之类的平台和Windows之间有所不同。

In this case the activate_this.py script adds the *nix style path: 在这种情况下,activate_this.py脚本会添加* nix样式路径:

VIRTUALENV_BASE/lib/python2.5/site-packages/ VIRTUALENV_BASE / lib中/的python2.5 / site-packages中/

to the python path instead of the Windows specific path: 到python路径而不是Windows特定路径:

VIRTUALENV_BASE\\Lib\\site-packages\\ VIRTUALENV_BASE \\ LIB \\站点包\\

I have created an issue in the virtualenv issue tracker which outlines the problem and the solution. 我在virtualenv问题跟踪器中创建了一个问题,该问题概述了问题和解决方案。 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 如果您有兴趣,可以在这里查看问题: 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. 希望这个修复程序将在virtualenv的upcomming版本中提供。


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. 如果您现在需要修复此问题,并且尚未修补virtualenv软件包,您可以“修复”您自己的activate_this.py,如下所示。

Edit your VIRTUALENV\\Scripts\\activate_this.py file. 编辑您的VIRTUALENV \\ Scripts \\ activate_this.py文件。 Change the line (17 ?): 换行(17?):

site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')

to

if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')

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. 有了这个,你的activate_this.py脚本将首先检查它正在运行的平台,然后定制site-packages目录的路径以适应。

Enjoy! 请享用!

You will have to execute the activate this, from within the fab file. 您必须从fab文件中执行activate this。 Altho' I have not tested it, I believe following should work: Altho'我没有测试过,我相信以下应该可以工作:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

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

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