1

Just reformatted my Mac to Yosemite and installed Python 2 & 3 using Homebrew. I've also setup some aliases in my bash_profile file which allows me to activate which version of Python I want to use. So if I type p3, it'll launch the python3 shell.

I'm wondering if it's possible to activate a specific version of Python without it starting the Python shell? So if I bring any files into the Terminal for instance, it'll use the version which I've activated?

Cheers!

Apologies if this has been answered elsewhere, I had a good search but I couldn't find anything.

2
  • 1
    Can you please explain "wondering if it's possible to activate a specific version of Python" Also what do you mean by "bring any files"? Run python script files? Commented Oct 18, 2014 at 18:15
  • If I type p3, I want it to use python3 without starting the shell for that specific Terminal session. So if I drag any files into the Terminal window, or open any files from commands, it starts it using Python 3 or 2 depending on what version I selected. Commented Oct 18, 2014 at 18:16

1 Answer 1

6

Rather than writing your own scripts to manage different Python versions, I would suggest using a highly-used manager that has been tested in and out by the community: pyenv. With pyenv you can:

  1. Easily install several different Python versions from the command line with no issues of them fighting (pyenv install 3.4.2)

  2. Create virtual environments from any one of those versions if you want to compartmentalize the packages that are available (pyenv virtualenv 3.4.2 mypy3projectvenv), and

  3. Set specific environments or versions to be active either
    • globally (pyenv global [version-or-venv]),
    • locally in and below folders you configure (pyenv local [ver-or-venv]), usually useful for projects that you have at a specific version/virtualenv, and
    • local to the shell until closed (pyenv shell [ver-or-venv]) (this is perhaps most-similar to your putative p3 command.

After setting the Python you want to use, all Python-related calls are redirected to their appropriate target (e.g. python, pip, easy_install, ipython*, django-admin*). Don't execute the Python scripts with any special command, just call them normally (or prefix a standard #!/usr/bin/env python shebang)

*If installed in that version/virtualenv

If you use the pyenv-installer script:

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

Then add the couple lines it tells you to your ~/.bash_profile script (and either source it or restart bash)...you'll be up and running in seconds. The trick is usually installing all the Python build dependencies with brew (sqlite, OpenSSH, zlib...), but after that then you're golden.

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

7 Comments

So basically install pyenv to "activate" different versions of Python in the Terminal, and then create a virtual environment if I need to (akin to Virtualenv I assume?), otherwise it'll use the default packages installed by brew? Sounds like perfect to me!! Thanks!
When you install Python versions with pyenv (and/or create virtualenvs from them) they are completely empty; you'll need to pip install what you need.
Brilliant, then I guess I can add the relevant pyenv commands to activate the different versions of Python more easily. Thanks for a very comprehensive answer. :)
Had a bit of a play around with pyenv and it seems to work well so far. Though I'm a bit confused with as to how virtualenv works with pyenv virtualenv. If I create a new environment for say django with pyenv virtualenv 3.4.2 django and I activate it with pyenv local django, it activates the environment but as soon as I cd into /.pyenv it deactivates it, and I need to change directory in order to start the server. Am I doing this wrong?
@user2942863 by /.pyenv do you mean ~/.pyenv? In normal use, you should never need to enter the .pyenv folder, the pyenv commands should be on your path (did you add the lines it told you to your .bash_profile?) and they manipulate what's in there. pyenv local [name] automatically will switch the env at or below that folder to that thing (it creates a .python-version file to mark it). Anything that would normally be on the path (e.g. django-admin) will be on the path, you shouldn't need to dig through ~/.pyenv/... to get stuff. Sometimes pyenv rehash might be needed though.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.