Activate python virtual environment using activate_this.py in file on Windows

I have a Fabric task that needs to access the settings of my Django project.

On Windows, I cannot install Fabric in the virtualenv project (problems with Paramiko + pycrypto options). However, I can install Fabric in my system-wide site packages, no problem.

I installed Django in the virtualenv project, and I can easily use all the "> python manage.py" commands when I activate virtualenv using the "VIRTUALENV \ Scripts \ active.bat" script.

I have a file with task files (fabfile.py) in my project that provides tasks for configuration, testing, deployment, etc. Some of the tasks in my file need to access the settings of my django project through "from django". conf import settings ".

Since the only Fabric program I use is in my system-wide site packages, I need to activate virtualenv in my file so that django becomes available. To do this, I use the "activate_this" module of the virtual project portal to have access to the project settings and the like. Using "print sys.path" before and after executing the activate_this.py function, I can say that the python path changes point to virtualenv for the project. However, I still cannot import django.conf.settings.

I was able to successfully do this on * nix (Ubuntu and CentOS) and on Cygwin. Do you use this setting / workflow on Windows? If so, can you help me understand why this does not work on Windows or provides any tips and tricks to work around this problem?

Thanks and greetings.


REF:

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)
+4
source share
2 answers

After some digging, I found out that this is a problem with activate_this.py script. In this current state, virtualenv <= 1.4.6, this script assumes that the path to the package site directory is the same for all platforms. However, the path to the site-packages directory is different from * nix-like platforms and Windows.

In this case, activate_this.py script adds the * nix style path:

VIRTUALENV_BASE / Library / python2.5 / site packages /

for the python path instead of the windows-specific path:

VIRTUALENV_BASE \ Lib \ site-packages \

I created a problem in virtualenv problem manager that describes the problem and the solution. If you are interested, you can check this problem here: http://bitbucket.org/ianb/virtualenv/issue/31/windows-activate_this-assumes-nix-path-to-site

We hope that the fix will be available in the quick release of virtualenv.


If you need to fix this problem right now, and the virtualenv package has not yet been fixed, you can "fix" your own activate_this.py, as shown below.

Edit the VIRTUALENV \ Scripts \ activate_this.py file. Change the line (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') 

At the same time, your activate_this.py script will first check which platform it is running on, and then adapts the path to the package sites directory to match it.

Enjoy it!

+6
source

You will have to activate this from the fab file. Altho 'I have not tested it, I believe the following should work:

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

Source: https://habr.com/ru/post/1307007/


All Articles