17

I'd like to play around in a virtual environment that's being interpreted purely via python3.3. On my system (Ubuntu 13.04), there are two ways to create a virtual environment.

virtualenv env

or:

pyvenv-3.3 env

If I use the old faithful, virtualenv, I'm able to use everything as expected, however, PIP installs everything into python2.7 libs rather than python3.3 libs. So, calling scripts using

python3 script.py 

Doesn't seem to work, yet:

python script.py 

Works like a charm. But, obviously must be using python2.7


Now, if instead, I use the "built-in" venv from Python3+ (pyvenv-3.3), it seems that everything get's a little whacky. It correctly places a python3.3 lib folder in the venv, however, installing modules using PIP is no longer possible as it seems to somehow reference the global rather than the virtual environment.

So, on to my question:

How would you recommend getting a working virtual environment with ONLY Python3.3(+) and PIP installing to python3.3 libs?

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • 1
    huh. weird. i ran `pyvenv-3.3 env` then `source env/bin/activate` then `wget http://python-distribute.org/distribute_setup.py` then `python distribute_setup.py` then `easy_install pip` then `pip install simple-date` and it's trying to install in the system libs. which sounds like the same issue. and this is on opensuse. seems like a bug somewhere. – andrew cooke Aug 10 '13 at 04:22
  • Yeah, seems to access system vs. virtual environment. Strange given that it's a venv. – Brandon Bertelsen Aug 10 '13 at 09:21

7 Answers7

52

You might consider trying something similar to the following:

  1. Create your virtual environment

    $ python3 -m venv myvenv
    $ source myvenv/bin/activate
    (myvenv)$
    
  2. Install pip according to the official pip installation instructions. As of pip v1.5.1 you no longer need to manually install setuptools before installing pip.

    (myvenv)$ curl https://bootstrap.pypa.io/get-pip.py | python
    
  3. Deactivate then reactivate your virtual environment. Do this or pip won't work correctly.

    (myvenv)$ deactivate
    $ source myvenv/bin/activate
    (myvenv)$ which pip
    

Longer response:

Yes, I had a similar problem. It has to do with all the other pythons and pips living on your system and the pip that did not get installed, like it does with the non "core" virtualenv you are used to.

For some reason the venv module does not install setuptools and pip.

I did the above and so far, so good.

venv in Python 3.3 does not install pip by itself:

Common installation tools such as Distribute and pip work as expected with venvs - i.e. when a venv is active, they install Python packages into the venv without needing to be told to do so explicitly. Of course, you need to install them into the venv first.

The Python 3.4 implementation of venv installs pip by default.

Changed in version 3.4: Installs pip by default

http://docs.python.org/3/library/venv.html

You can check your pip with which pip and pip -V. If you did not deactivate/activate the venv and pip -V will not show the correct the correct pip, but which pip may still show the correct path.

Nick Chammas
  • 11,843
  • 8
  • 56
  • 115
Matt Schlobohm
  • 1,312
  • 12
  • 15
  • I had a similar solution to Matt's except I'm on a mac. The steps are pretty much the same though. I blogged about the steps here: http://blog.troygrosfield.com/2014/01/09/python3-virtual-environments/ – Troy Grosfield Jan 09 '14 at 14:29
  • 1
    Works like charm on Ubuntu 12.04 LTS – amertkara Jan 20 '14 at 21:27
  • 3
    Might be worth noting that the venv should be re-activated before using pip, otherwise the version of pip used will still be the system version – Robert Kleinsasser Jan 29 '14 at 04:35
  • I am using python3.6 and pip gets installed by default. 'python3.6 -m venv '. source path/to/install/bin/activate , then ran pip --version – Py_minion Jan 03 '17 at 20:15
7

Here is how I did it (I am on Ubuntu 12.04).

First, install Python 3.3 in a folder, for example /opt/python3. Then run the following commands:

/opt/python3/bin/pyvenv-3.3 ~/py3  # pyvenv is python3's built-in virtualenv
source ~/py3/bin/activate
curl https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py | python
easy_install-3.3 pip 
argentpepper
  • 4,202
  • 3
  • 33
  • 45
Kenneth Jiang
  • 589
  • 7
  • 13
5

The latest version of pip has a pip3 function that will install into your default Python3 instance.

pip3 install python_lib

If you have both Python 2.x and 3.y on your system, pip will install into your Python 2.x and pip3 into Python 3.y.

Thane Plummer
  • 7,966
  • 3
  • 26
  • 30
2

this works for me:

> virtualenv-3.3 env
...
> source env/bin/activate
> pip install simple-date
...
> python
Python 3.3.0 (default, Mar 27 2013, 20:56:52) 
[GCC 4.7.2 20130108 [gcc-4_7-branch revision 195012]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import simpledate
>>>

unfortunately, i'm not completely sure how i installed virtualenv-3.3 back in the day. i think i probably did something like:

> wget http://python-distribute.org/distribute_setup.py
> python3.3 distribute_setup.py
> easy_install-3.3 virtualenv

[edit: yes, that seems to be confirmed by my notes at http://www.acooke.org/cute/GettingPyt0.html ]

note that is then a python-3.3 specific virtualenv.

as described in the comments, i could not get pyvenv-3.3 to work.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • +1 However, I'm really looking for a working solution with pyvenv-3.3 as there are a few features of the new built in venv that I'm interested in playing with. Appreciate the attempt either way. – Brandon Bertelsen Aug 10 '13 at 09:23
1

I had the same issue on OS X. It looks like even the pip executable that gets put in bin/ by easy_install is still meant for Python 2. If you use the pip3 executable (or variant thereof) it should work just fine!

Rayne
  • 31,473
  • 17
  • 86
  • 101
1

I have used the PPA ppa:fkrull/deadsnakes to install Python 3.4 in my Elementary Os (a Ubuntu based Linux distribution).

I would like to make one indication. In the Python 3.4 Ubuntu package **when you create a a virtual environment (with pyvenv-3.4) pip is installed under local/bin instead of bin.

So to use it you have to modify the bin/activate script at line 44. It should be

PATH="$VIRTUAL_ENV/bin:$VIRTUAL_ENV/local/bin:$PATH"
Ciges
  • 1,133
  • 11
  • 17
0

It's well mentioned in the docs:

Common installation tools such as Distribute and pip work as expected with venvs - i.e. when a venv is active, they install Python packages into the venv without needing to be told to do so explicitly. **Of course, you need to install them into the venv first: this could be done by running distribute_setup.py with the venv activated, followed by running easy_install pip**. Alternatively, you could download the source tarballs and run python setup.py install after unpacking, with the venv activated.

wassimans
  • 8,382
  • 10
  • 47
  • 58