Ask HN: People using both python 2.x and 3.x on OS X, what setup do you use?

2 points by natch ↗ HN
Specifically I'm wondering how you make it all work with installing and using modules, when a few things are happening:

- You may need to run 2.7 scripts

- You may need to run 3.x scripts

- You may need to edit, create, and maintain scripts of both versions.

- The system may change the system python out from under you, with major updates of the OS or Xcode

- You install libraries with pip or pip3 and they claim to have installed for a specific version of python, but then at run time, that same specific version of python complains that the library isn't installed.

What setup is best for managing this?

If your answer is "I don't do that" or "I wouldn't do that if I were you" then it's OK, you don't have to answer... this question is for people who are doing it.

4 comments

[ 2.3 ms ] story [ 22.2 ms ] thread
About three months ago I started a serious attempt to port my projects from Python v2 to v3. Although I do not deploy on OS X, I do a fair bit of development on OS X.

My setup: 1) never use the system Python for development. 2) install Python 2.7.10 and 3.4.3 from mac ports. 3) Always work in a virtual environment. 4) When developing generic scripts, use the port select method to switch between versions. 5) Have virtual environments built out for both versions.

What I've not yet done is write a script to build a binary 2.7 and 3.4 wheel. In that case, I'd likely just refer to the 2.7 and 3.4 versions specifically instead of generically (ie. python2.7 and python3.4 vs. just python).

Thanks! Good to hear someone is making it work. I'm studying your reply (and that of citruspi) and am going to take a stab at this.
I develop for both Python 2 and Python 3 on OS X and my setup is pretty similar to rgacote's.

I installed both versions of Python using homebrew[0] so it doesn't matter which version OS X ships with.

Python 2 is still the default. So I have python and pip, and then python3 and pip3.

Every project I work on has a virtual environment[1], which is named after the owner and the project, e.g. citruspi-skynet.

If it's a Python 2 project, I just create the virtual environment normally.

    mkvirtualenv citruspi-skynet
If it's a Python 3 project, I set the location of Python interpreter.

    mkvirtualenv -p $(which python3) citruspi-skynet
I've run into situations where I have to port a Python 2 project to Python 3, while maintaining the Python 2 project. In that case, I normally use the same repository, but a new branch. However, I'll keep two copies of the repository locally so I can work on both branches at the same time.

    Code/
        citruspi/
            skynet
            skynet3
And then I'll have two virtual environments, citruspi-skynet and citruspi-skynet3.

I haven't run into any issues with this setup and I've been pretty happy.

[0]: http://brew.sh

[1]: https://virtualenv.pypa.io/en/latest/

Thanks! Did not know about virtualenv, I'll try that. This is really good and detailed information; I appreciate it (and rgacote's too).