y0prst
y0prst

Reputation: 800

virtualenv and 'python -S' issue

Look at this log:

This works:

$ python -c 'import Queue'
$ python -S -c 'import Queue'

But not with virtualenv:

$ virtualenv v1
$ source v1/bin/activate
(v1)$ python -c 'import Queue'
(v1)$ python -S -c 'import Queue'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named Queue

Same story with zipfile, argparse, optparse, shutil, subprocess, urllib. On the other hand, bunch of system libraries are working (os, datetime, sys, stat, re).

I experienced it on OS X "brewed" python, and also on linux and windows (tried only optparse, sorry).

Is it a bug of virtualenv?

UPDATED: I also tried virtualenv --system-site-packages with the same result.

UPDATED: If you think it's not bug, tell why? Particularly, why I lose subprocess with -S option and don't lose datetime? What is so special about half of the modules?

Upvotes: 2

Views: 466

Answers (2)

karel
karel

Reputation: 5862

It's not a bug of virtualenv. You're getting the error message because of the -S option after python in python -S -c 'import Queue' when it is used in the virtualenv Python virtual environment.

-S     Disable the import of the module site and the site-dependent
       manipulations of sys.path that it entails.

The site module is automatically imported during initialization. The automatic import can be suppressed using the python interpreter's -S option. Importing this module will append site-specific paths to the module search path and add a few builtins, unless -S was used.

Regarding your question about why you lose subprocess with -S option and don't lose datetime: The difference is that datetime is a built-in module in Python and Queue is not. If you check in your vi folder, you will find that python is already installed in vi/bin/python (by virtualenv in Linux). However Queue is not installed anywhere in vi or its subdirectories unless you installed Queue locally in vi (e.g. with pip install).

Upvotes: 2

Tanin
Tanin

Reputation: 1933

The root cause is that virtualenv's list of built-in modules is much smaller than the system python's list.

The problem occurs with optparse, subprocess, and many other "built-in" (or so we thought) modules as well.

Read more here: http://tanin.nanakorn.com/blogs/362. The explanation is long.

Upvotes: 1

Related Questions