Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Virtual Environment and PIP

Tags:

python-3.x

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?

like image 724
Brandon Bertelsen Avatar asked Aug 10 '13 00:08

Brandon Bertelsen


People also ask

Does virtualenv have pip?

Virtualenv sets up the Python environment and installs the pip program. When the environment is active, the command prompt begins with (project), where project is the environment name.

What is python3 virtual environment?

Overview. Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).

Is pip an environment?

Pip is the default package manager for Python that most developers use to manage their Python global and virtual environments by installing, updating and uninstalling packages and their dependencies. By default, pip installs packages located in the Python Package Index (PyPI), but can also install from other indexes.


2 Answers

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.

like image 116
Matt Schlobohm Avatar answered Oct 17 '22 09:10

Matt Schlobohm


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 
like image 38
Kenneth Jiang Avatar answered Oct 17 '22 09:10

Kenneth Jiang