Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows, activate_this assumes *nix path to site-packages #43

Closed
vbabiy opened this issue Mar 14, 2011 · 5 comments
Closed

Windows, activate_this assumes *nix path to site-packages #43

vbabiy opened this issue Mar 14, 2011 · 5 comments
Labels

Comments

@vbabiy
Copy link

vbabiy commented Mar 14, 2011

==== The problem

When I attempt to use the activate_this.py script to activate an environment on Windows, the script appears to have correctly reworked the python path through sys.path. However, I am unable to import any module installed to the virtualenv's site-packages.

==== The cause

I believe this is because the activate_this.py script assumes the path to the "site-packages" directory is the same on all platforms, but it is not. The location of the site-packages directory in virtualenvs on Windows is different from the location of the same directory on *nix type OSes.

On *nix type platforms, the location of site-packages is a directory on a path that is dependent on the Python version that was used to create the virtualenv.

So assuming the virtualenv was created with Python2.5, the activate_this.py script should use the location:

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

On the other hand, with virtualenvs on Windows, the site-packages directory is located at:

*VIRTUALENV_BASE\Lib\site-packages*

==== Possible solution

I think this could be resolved with a simple check like:

#!python
if sys.platform == 'win32':
    # use windows site-packages path
else:
    # use *nix style site-packages path

==== Steps to re-create

Assumptions:

  • Your development setup is a Windows box with Python (2.5 or 2.6) installed
  • You have virtualenv<=1.4.6 installed

1. Create an isolated virtualenv and install a simple package in it:

#!console
> virtualenv --no-site-packages mytestenv
> mytestenv\Scripts\activate.bat
(mytestenv) > pip install Yolk
(mytestenv) > yolk --list
pip             - X.Y.Z        - active
setuptools      - X.Y.Z        - active
yolk            - X.Y.Z        - active
(mytestenv) > deactivate
>

2. Verify that you can import the module in the python shell when the virtualenv is activated with the batch file.

#!console
> mytestenv\Scripts\activate.bat
(mytestenv) > python



#!python
>>> import yolk
>>> 

3. Create a python test script to activate the environment in python and import the "Yolk" module

#!python
import os

# Activate the environment with activate_this.py
here = os.path.abspath(os.path.dirname(__file__))
activate_this = os.path.join(here, 'mytestenv', 'Scripts', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

import yolk

Save the file as smalltest.py and try to run it.

IMPORTANT -- to recreate this issue properly, ensure that your virtualenv has been deactivated prior to running the smalltest.py script.

#!console
> python smalltest.py
Traceback (most recent call last):
    ...
ImportError: No module named yolk

Observe that the import failed because the virtualenv was not properly activated by executing the activate_this.py script.


Local development environment:

  • Python 2.5.4
  • Virtualenv 1.4.6
  • Windows XP (SP3)

If you are interested in how I discovered this problem, you may refer to :


@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

  • Changed title from The "activate_this.py" script assumes a *nix like path
    to site-packages on Windows to Windows, activate_this assumes a *nix like path
    to site-packages.

Original Comment By: Rudy Lattae

@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

  • Changed title from Windows, activate_this assumes a *nix like path to
    site-packages to Windows, activate_this assumes *nix path to site-packages.

Original Comment By: Rudy Lattae

@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

To get the ball rolling on this issue, I implemented and tested what I hope is
a decent fix for the issue. The fix is to have activate_this.py check which
platform it is running on before it crafts the path to the site-packages
directory.

NOTE: I only tested this on Windows XP (32bit) I have not tested it on Vista,
7, or any 64 bit versions.

Hope it helps.


Original Comment By: Rudy Lattae

@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

Fix #31, make activate_this.py work on Windows (use Lib/site-packages)

→ [Author: ianb

Age: 11 months ago

Fix 31, make activate_this.py work on Windows (use Lib/site-
packages)">r296:9d4f196d2bf4]2

assumes-nix-path-to


Original Comment By: Ian Bicking

@vbabiy
Copy link
Author

vbabiy commented Mar 14, 2011

Note I haven't tested this change myself, if it didn't fix the problem please
reopen this ticket.


Original Comment By: Ian Bicking

@pypa pypa locked and limited conversation to collaborators Jan 14, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant