0

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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

If you've read my entire question, I did use pythonbrew. The only difference is I used the use command to switch to the python version instead of the switch command to create a venv. When I used the use command and created a venv, the venv would have the older version, and not the one I used in the use command. Is the problem related to me using the use command as opposed to switch as you've written it above?
Actually I thought the problem was more around trying to use virtualenvwrapper, that's why I provided my alias for workon that does work in that case. Once the venv is created you should be able to switch to something else and when you activate the venv you end up with the right version in that environment. Might be that pythonbrew use is for the current command, similar to LD_LIBRARY=foo runsomething, and switch is more like export LI_LIBRARY=foo. But once you create the virtualenv, I would imagine it would activate and run with the right python.
When I used your instruction: pythonbrew venv create proj | It created a new python (2.7.6) in this folder tree: user/.pythonbrew/venvs/Python-2.7.6/proj ... How do I now install things in this virtual environment? Should I work from the Python-2.7.6 folder that is within venvs? Normally with pythonbrew I'd work from any folder, but for this, it seems like you need to work within this specific folder and make your installs of extra tools/libraries here too. Your feedback will be much appreciated.
Once you activate the virtualenv, pip will install into it, regardless of your current directory when you do the install. Activation is key here.
Thanks Mark, I figured that from the: "pythonbrew venv use proj" command. In your file/folder tree, you show that your actual app code is within the project folder itself. Is this the best way to use venv? I was thinking of keeping my app code elsewhere, but that seems to defeat the purpose of a virtual environment. Also, when using a command to create some app with files/folders while in the commandline virtual environment, will the code to generate files/folders automatically go to the project folder?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.