As an aside, today I was hunting around for Python version management à la RVM and I found pyenv.
For version management it’s pretty straightforward. To install on the Mac, I just used Homebrew:
brew install pyenv
Since the version of Python on my Mac (running OS 10.10.4) is 2.7.6, I used pyenv to install 2.7.10 and 3.4.3:
pyenv install 2.7.10 pyenv install 3.4.3
A little backwards, but at this point I realized I needed to update my .bash_profile per the installation instructions:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Ubuntu users: according to the instructions you should use .bashrc not .bash_profile. Verify your installation instructions on the GitHub README.
If you’re new(ish) to BASH, before sourcing .bash_profile take a look at the new lines that you just added. Then, source:
source ~/.bash_profile
If you take a look where python is run, you'll see that it has changed from:
$ which python /usr/bin/python
To:
$ which python /Users/quinn/.pyenv/shims/python
Now a cool thing that you can do with pyenv is use .python_version file to specify what version of Python you want used with your script.
To play with this, create a .python_version file with the version number:
2.7.10
Now create a quick little Python script, myscript.py:
username = input('What is your Reddit username? ')
name = input('What is your name IRL? ')
age = input('How old are you? ')
print("Your name is ", name, ", you are ", age, " years old, and your Reddit username is ", username, ".")
When you run your script (python myscript.py) it should fail because the script is not compatible with Python 2.x. Update the .python_version file to 3.4.3 instead of 2.7.10 and re-run. This time you should be successful.
Protip: If you need to run against a version of Python that you haven’t yet installed, you can run pyenv install without any arguments from the directory with your .python_version file. This will install whatever versions of Python are specified in the file.
