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?

martineau
  • 119,623
  • 25
  • 170
  • 301
darksky
  • 20,411
  • 61
  • 165
  • 254

1 Answers1

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.

boatcoder
  • 17,525
  • 18
  • 114
  • 178
  • 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? – darksky Feb 19 '13 at 14:57
  • 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. – boatcoder Feb 19 '13 at 20:26
  • 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. – Joe Jan 13 '14 at 13:00
  • Once you activate the virtualenv, pip will install into it, regardless of your current directory when you do the install. Activation is key here. – boatcoder Jan 13 '14 at 18:06
  • 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? – Joe Jan 13 '14 at 19:11
  • There is no problem putting the venv in another folder, I keep mine in the same folder for a variety of reasons, greping the source tree being one of them. The ability copy them wholesale (via tarballs) from one machine to another. And for CI with jenkins which needs to build out the venv if it isn't current. I ALWAYS run manage.py from the project folder. I think it will create things in the current directory, but I really don't know. – boatcoder Jan 13 '14 at 19:44
  • "There is no problem putting the venv in another folder" I was actually referring to app files. Say you are building a django app, would your django files stay in the venv proj folder or can you keep your django files anywhere you like? Sorry if I misunderstood, as it isn't that clear to me. – Joe Jan 14 '14 at 00:03
  • You have to keep all your django files together, but they don't have to go into the venv. I find it tidier. When I get ready to delete a project or tar it up, or search it, or whatever, everything in one place works well for me. But I know pyDanny keeps his envs separate from his code. All a matter of preference. At any given moment I have 10+ projects that are "active" – boatcoder Jan 14 '14 at 04:46