47

I'm running Precise Pangolin amd64. I installed Python 3.3 from ppa:fkrull/deadsnakes.

Now I want to actually use this new Python version in a virtualenv. But How can I do this? I get the following error:

$ virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Running virtualenv with interpreter /usr/bin/python3.3
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3
Also creating executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/site.py", line 73, in <module>
    __boot()
  File "/usr/local/lib/python2.7/site-packages/site.py", line 2, in __boot
    import sys, imp, os, os.path   
ImportError: No module named 'imp'
ERROR: The executable /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 is not functioning
ERROR: It thinks sys.prefix is '/home2/USERNAME/.virtualenvs' (should be '/home2/USERNAME/.virtualenvs/pywork3')
ERROR: virtualenv is not compatible with this system or executable

If instead, I explicitly use python3.3 to call virtualenv, I get this error:

$ python3.3 /usr/bin/virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 2, in <module>
    import virtualenv
ImportError: No module named 'virtualenv'

I'm stuck. Any help is greatly appreciated!

EDIT: Following the advise by @thefourtheye, I purged my local site-packages directory (effectively only deleting site.py). Now I'm getting a step further, but virtualenv complains about a missing easy_install:

 Error [Errno 2] No such file or directory:   
'/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install' while executing command    
/home2/USERNAME/.virt...rk3/bin/easy_install /usr/share/python-virtualenv/pip-1.1.tar.gz

I did install the package python3-setuptools, which installs the Py3 version of easy_install.

EDIT2:

Here's the verbose output, without explicitly passing --distribute and --no-site-packages, as these two switches are default behaviour of my virtualenv:

$ virtualenv --verbose -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
Running virtualenv with interpreter /usr/bin/python3.3
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3
Symlinking Python bootstrap modules
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/config-3.3m
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/lib-dynload
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/os.py
  Ignoring built-in bootstrap module: posix
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/posixpath.py
  Cannot import bootstrap module: nt
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/ntpath.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/genericpath.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/fnmatch.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/locale.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/encodings
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/codecs.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/stat.py
  Cannot import bootstrap module: UserDict
  Cannot import bootstrap module: copy_reg
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/types.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/re.py
  Cannot import bootstrap module: sre
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_parse.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_constants.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/sre_compile.py
  Ignoring built-in bootstrap module: zlib
  Cannot import bootstrap module: _abcoll
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/warnings.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/linecache.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/abc.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/io.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/_weakrefset.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/copyreg.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/tempfile.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/random.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/__future__.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/collections
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/keyword.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/tarfile.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/shutil.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/struct.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/copy.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/base64.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/bisect.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/_dummy_thread.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/hashlib.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/heapq.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/hmac.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/reprlib.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/rlcompleter.py
  Symlinking /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/weakref.py
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/site-packages
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/site.py
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/orig-prefix.txt
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/no-global-site-packages.txt
Creating parent directories for /home2/USERNAME/.virtualenvs/pywork3/include
Symlinking /home2/USERNAME/.virtualenvs/pywork3/include/python3.3m
Creating /home2/USERNAME/.virtualenvs/pywork3/bin
New python executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3
Changed mode of /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 to 0o755
Also creating executable in /home2/USERNAME/.virtualenvs/pywork3/bin/python
Changed mode of /home2/USERNAME/.virtualenvs/pywork3/bin/python to 0o755
Testing executable with /home2/USERNAME/.virtualenvs/pywork3/bin/python3.3 -c "
import sys
prefix = sys.prefix
if sys.version_info[0] == 3:
    prefix = prefix.encode('utf8')
if hasattr(sys.stdout, 'detach'):
    sys.stdout = sys.stdout.detach()
elif hasattr(sys.stdout, 'buffer'):
    sys.stdout = sys.stdout.buffer
sys.stdout.write(prefix)
"
Got sys.prefix result: '/home2/USERNAME/.virtualenvs/pywork3'
Creating /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils/__init__.py
Writing /home2/USERNAME/.virtualenvs/pywork3/lib/python3.3/distutils/distutils.cfg
Using existing distribute egg: /usr/share/python-virtualenv/distribute-0.6.24.tar.gz
Installing distribute..............................................................................................................................................................................................................................................................................................................................................................................................................done.
Installing existing pip-1.1.tar.gz distribution: /usr/share/python-virtualenv/pip-1.1.tar.gz
Installing pip...
  Error [Errno 2] No such file or directory: '/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install' while executing command /home2/USERNAME/.virt...rk3/bin/easy_install /usr/share/python-virtualenv/pip-1.1.tar.gz
...Installing pip...done.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 2283, in <module>
    main()
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 938, in main
    never_download=options.never_download)
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 1054, in create_environment
    install_pip(py_executable, search_dirs=search_dirs, never_download=never_download)
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 643, in install_pip
    filter_stdout=_filter_setup)
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 976, in call_subprocess
    cwd=cwd, env=env)
  File "/usr/lib/python3.3/subprocess.py", line 818, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.3/subprocess.py", line 1416, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/home2/USERNAME/.virtualenvs/pywork3/bin/easy_install'
3
  • Just for clarity's sake, can you try this command (excluding distribute): virtualenv --no-site-packages -p /usr/bin/python3.3 ~/.virtualenvs/pywork3?
    – don.joey
    Apr 11, 2013 at 12:53
  • I tried with --setuptools as well, and the result is exactly the same (including the installation of distribute).
    – andreas-h
    Apr 11, 2013 at 13:36
  • In 14.04 everything works painlessly. Sep 30, 2014 at 9:31

7 Answers 7

59

Python 3.3 has venv built-in.

http://docs.python.org/3/library/venv.html#module-venv

Simply run

pyvenv-3.3 /path/to/environment

And then to activate it

source /path/to/environment/bin/activate

This built-in version of virtualenv is much more flexible than what you're probably used to. For example, you can extend EnvBuilder to do pretty much whatever you want. You can copy an example implementation of EnvBuilder from the link below and play around with it: http://docs.python.org/3/library/venv.html#an-example-of-extending-envbuilder

That script above likely does most of what we expect to get out of virtualenv. So if you just need a virtualenv with easy_install and pip, you should be good to go.

See @MarkOfSine's edits below for clarification on how to get running if you're still confused.


To add to the above, and as per docs:
For example, after executing: pyvenv-3.3 /path/to/my_project/venv
You can run distribute_setup.py, which seems to do various things, but essentially you end up with easy_install in your ./my_project/venv/bin directory.
This can then be used to install pip and the like.

It does not say where you should get distribute_setup.py from, so I downloaded from:

http://python-distribute.org/distribute_setup.py

and using the activated environment:

cd /path/to/my_project
source venv/bin/activate

ran :

python distribute_setup.py

and

easy_install pip

Which then completed the setup of the virtual environment more inline with virtualenv on python 2.x

5
  • 2
    nice one - i wasn't aware of this ...
    – andreas-h
    Oct 10, 2013 at 6:52
  • 1
    actually, this seems not to be a "real" virtualenv. There's neither pip nor easy_install available after activating it.
    – andreas-h
    Oct 10, 2013 at 9:53
  • @andreas-h You need to install those yourself. That doesn't make it not "real".
    – Chris
    Oct 13, 2013 at 23:24
  • 1
    @andreas-h Also, it's designed so that you can create a script to add whatever you want yourself.. see docs.python.org/3/library/…
    – Chris
    Oct 13, 2013 at 23:27
  • When you terminal keeps yelling about wrong permissions when installing pip, check if you are using the correct binary of easy_install. You can select the binary in the virtualenv explicitly by ./venv/bin/easy_install pip.
    – OrangeTux
    Dec 17, 2013 at 16:00
50

It is easier than as it seems:

virtualenv -p /usr/bin/python3 yourenv
source yourenv/bin/activate
pip install package-name

really works :)

2
  • 5
    That should be correct answer Feb 26, 2016 at 13:02
  • Yeah ! That was super easy ! Thanks man. @Cem Yildiz May 10, 2017 at 4:15
25
  1. Take backup of site-packages.

    cp -r /usr/local/lib/python2.7/site-packages/ /tmp/site-packages

  2. Truncate that directory

    rm -rf /usr/local/lib/python2.7/site-packages/

  3. Now try the same

    virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3

  4. To install without pip

    virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --no-pip

EDIT: Looks like the version of virtualenv (1.7.1.2) and python3.3 are not compatible. So, please try this

  1. Uninstall python-virtualenv using sudo apt-get remove python-virtualenv

  2. wget http://peak.telecommunity.com/dist/ez_setup.py; sudo python ez_setup.py This will install latest easy_install.

  3. sudo easy_install pip

  4. sudo pip install virtualenv

  5. virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3

10
  • interesting, now I'm getting a step further, but still not all the way there. See my updated question
    – andreas-h
    Apr 11, 2013 at 12:20
  • I am going through the virtualenv code. It would be helpful, if you could execute the same command with increased verbosity and show us the complete output. virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3 --verbose Apr 11, 2013 at 12:58
  • got it, see my updated question.
    – andreas-h
    Apr 11, 2013 at 13:37
  • Have been analyzing for more than two hours. Looks like distribute doesnt install the easy_install scripts in bin directory. We can install them manually later. So just to install virtualenv, try to install without pip, as I have updated in the answer Apr 11, 2013 at 15:20
  • 1
    Instead of installing virtualenv manually, I just downloaded the .dev packge for Ubuntu Raring from packages.ubuntu.com/raring/all/python-virtualenv/download and installed it via dpkg -i. Seems to work now. Thanks a lot!
    – andreas-h
    Apr 12, 2013 at 8:25
11

You are doing this far too complicated. If it's included in python3, just create it with :

python3 -m venv

You can even create an alias if you wish to

alias virtualenv3='python3 -m venv'

Depending on the machine, you might need to specify the interpreter:

alias virtualenv3='python3 -m venv -p python3'
3
  • 1
    The package python3-venv is also needed, at least on Ubuntu 14.04.
    – gioele
    Nov 16, 2016 at 14:31
  • For me (also on 14.04), the package was called python3.4-venv.
    – tobek
    Mar 16, 2017 at 8:59
  • on version 18.04, too
    – fanny
    Nov 8, 2019 at 11:17
2

The steps that worked for me: On MAC OSX 10.9.5

  1. Download and install Python-3.4.3 manually.

    P3PATH=/Users/$USER/Python3
    mkdir -p $P3PATH
    cd $P3PATH
    tar -zxvf Python-3.4.3.tgz
    ./configure --prefix=$P3PATH/Python-3.4.3
    make; make install
    
  2. Create a virtualenv.

    $P3PATH/Python-3.4.3/bin/pyvenv py3env
    source py3env/bin/activate
    

Note:
virtualenv -p <path to python3> py3env did not work:

Error due to zlib
2
  • This also works fine on Debian 7 (wheezy). Just another P3PATH
    – rubo77
    Jun 19, 2015 at 16:56
  • For the debian based distro in-a-container users, make sure to install libssl-dev and zlib1g-dev so that python is compiled with _ssl.so zlib.so Oct 21, 2017 at 14:22
1
On Ubuntu 14.04 

sudo apt-get install python3-pip

sudo pip install virtualenv

virtualenv-3.4 --no-site-packages venv
0

just be sure that you installed pip and venv:

sudo apt install python3-pip
sudo apt install python3-venv

Then you can use it like this:

python3 -m venv ~/env-sample
. ~/env-sample/bin/activate

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .