activate_this.py does not update PATH environment variable #14

Closed
vbabiy opened this Issue Mar 14, 2011 · 3 comments

3 participants

@vbabiy

I want to activate virtualenv and execute some application, which is located at the virtualenv's bin directory:

#!python
import os, subprocess

virtual_env = './python'
bin_dir = os.path.join(virtual_env, 'bin')
# `pip` here
assert os.access(os.path.join(bin_dir, 'pip'), os.X_OK)

activate_this = os.path.join(bin_dir, 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
subprocess.call(['pip', '--version'])

But I got "OSError: [Errno 2] No such file or directory"

When I add bin directory to the PATH environment variable, then it works.

#!python
virtual_env = './python'
activate_this = os.path.join(virtual_env, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
os.environ['PATH'] = os.pathsep.join([
  os.path.join(virtual_env, 'bin'),
  os.getenv('PATH', '')])
subprocess.call(['pip', '--version'])

This prints "pip 0.8.1 from /tmp/python/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg (python 2.6)"

Why an activate_this.py script doesn't set PATH for me, like any other bin/activate* scripts?


@vbabiy

It looks like the activate.sh shell script updates your PATH.


Original Comment By: Kelsey Hightower
@vbabiy

Looking at the docs, the "Using Virtualenv without bin/python" section should
give you more details on what activate_this.py does.

http://virtualenv.openplans.org/#using-virtualenv-without-bin-python


Original Comment By: Kelsey Hightower
@dougn

This should still update the environment to add bin (or Scripts on windows) to your PATH. This is in keeping with the principal that this activation will extend/override, but not replace a previous activation as denoted in the documentation. It will meanrly cause items to first be searched for in this environment which includes BOTH the modules, and the top level scripts.

To do otherwise causes many more problems. For instance if you use activate_this.py and then try to spawn a call to sphinx-build, it will use the executable from the old env, as that is what is on the PATH, but the module from the new env, as that is what is on the PYTHONPATH.

This is patently wrong.

@dougn dougn pushed a commit to dougn/virtualenv that referenced this issue Dec 17, 2011
Douglas Napoleone Fix for issue #14 Have activate_this.py update the environment PATH a…
…s well as the PYTHONPATH as the other activate items do.
c84deb4
@dougn dougn added a commit to dougn/virtualenv that referenced this issue Dec 17, 2011
@dougn dougn fixing a stupid type. Helps to test. This should now fix #14 without …
…crashing.
9af2b77
@carljm carljm closed this in c6f5950 Dec 17, 2011
@dougn dougn added a commit to dougn/virtualenv that referenced this issue Dec 17, 2011
@dougn dougn redoing the fixing #14 after a repro rebase because I cant use git, a…
…nd messed up my fork.
172c586
@gregglind gregglind added a commit to gregglind/virtualenv that referenced this issue Jan 2, 2012
@carljm carljm Fixed #14 -- modify environ PATH in activate_this.py. Thanks Doug Nap…
…oleone.
7e571d6
@blueyed blueyed added a commit to blueyed/virtualenv that referenced this issue Oct 31, 2013
@blueyed blueyed activate_this.py: do not duplicate first PATH entry
If the bin dir is already in $PATH, do not prepend it again.
This might happen after `workon` had been used and the virtualenv gets
activated in e.g. Vim again via the Python API.

Ref: pypa#14
fff4596
@blueyed blueyed added a commit to blueyed/virtualenv that referenced this issue Oct 31, 2013
@blueyed blueyed activate_this.py: do not duplicate first PATH entry
If the bin dir is already in $PATH, do not prepend it again.
This might happen after `workon` had been used and the virtualenv gets
activated in e.g. Vim again via the Python API.

Ref: pypa#14
3e86ec9
@blueyed blueyed added a commit to blueyed/virtualenv that referenced this issue Oct 31, 2013
@blueyed blueyed activate_this.py: do not duplicate first PATH entry
If the bin dir is already in $PATH, do not prepend it again.
This might happen after `workon` had been used and the virtualenv gets
activated in e.g. Vim again via the Python API.

Ref: pypa#14
Fixes #493
f67cce0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment