Manage python version in different virtualenv with

2019-06-10 04:49发布

问题:

My main system python version is 2.7.3. I'm trying to create a virtualenv that uses version 3.3.0. I installed pythonbrew, virtualenv and virtualenvwrapper.

I followed this tutorial: http://technomilk.wordpress.com/2011/07/27/setting-up-our-django-site-environment-with-pythonbrew-and-virtualenv/

Which essentially runs pythonbrew use 3.3.0, and then create a virtualenv. The tutorial says that the virtualenv will use the version pythonbrew uses. But it doesn't. The virtualenv uses 2.7.3 when I start it. When I do pythonbrew use 3.3.0, it leaves the virtualenv and it applies to the system instead of the environment.

Apparently, pythonbrew has its own virtualenv wrapper, which has a tutorial at: http://suvashthapaliya.com/blog/2012/01/sandboxed-python-virtual-environments/

I hate doing it like that though. Is it possible to use virtualenvwrapper along with pythonbrew (and not pythonbrew's venv wrapper) to be able to choose which python version to use for each venv, and keep them separate from the system python version?

Also, I do not want to use mkvirtualenv -p flag, as this means I need to manually install python3.3. I'd rather stick to using a package manager to manage python versions. It's hard to believe that nothing in python equates to RVM in ruby... Unless I'm mistaken?

回答1:

Maybe you should look http://pypi.python.org/pypi/pythonbrew/ instead. When I did it, I used pythonbrew to create the venv

pythonbrew install 2.7.3
pythonbrew switch 2.7.3
pythonbrew venv create proj

Worked like a champ.

I've taken to creating my virtual environments in a .folder underneath my git repo so that I can dispose of the virtual environment without messing with my code and rebuild it if I so desire. I bumped into this technique while working with jenkins which does the git clone for you, then you have to figure out how to build a virtual environment around it.

Python/proj
    .proj           <---- Virtual environment is in here!
        lib
        site-packages
    settings
    requirements
    apps

I also have a bash function that does workon for me.

function workon() {
     if [ -d ~/Python/$1 ]
     then
            cd ~/Python/$1
            if [ -d .$1 ]
            then
                . .${1}/bin/activate
            else
                . bin/activate
                cd $1
            fi
     fi
}

This one is overly complicated to deal with old projects where the clone was done inside the virtual environment as well as the new ones where the virtual environment is inside the project.