Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching between python 2 and 3 versions while scripting

I usually develop with python 2.7, but would like to start creating some tools in python 3.x. What is the easiest way to have both running side-by-side, while keeping some semblance of control over what libraries I have installed where...

If I use pyenv to switch between versions, will it propagate to a generic shebang line? Something like

#!/usr/bin/env python

or even better, can I specify which python in the shebang?

#!/usr/bin/env python3

I am anticipating a lot of "Use virtualenv" replies. Is this really the only way to do it? I feel like I would like to have the "base" python on my system with whatever libraries I have installed so that I can change between the two environments by typing something simple like pyenv global 3.2.3

I am using OSX, Mountain Lion at the moment.


Trying to explain it a little better, I have two alternative questions:

  • If I use something like virtualenv, will I lose the ability to run python2 and python3 scripts alternately, without changing the environment (i.e., just via shebang)?

  • In contrast, if I use two independent version installations, how can I control/know what will be installed by pip or easy_install for example.


UPDATE: Currently using python3 in the shebang line, and using pip3 to install packages to python3... Seems to work fine.

like image 337
beroe Avatar asked Oct 22 '22 02:10

beroe


1 Answers

You have a few possible methods varing slightly with os:

  1. Invoke python 2 as python and 3 as python3
  2. Extensions of .py and .py3
  3. Virtualenv
  4. A script or batch file to switch envionments. I use this to test my code against different versions of python 2. Note that you can have different envionments set in separate command sessions at the same time.
  5. Testing in virtual machines.
  6. Use an IDE that allows you to specify python versions on a project or debug session basis I use WingIDE but it is not the only one to allow this.

It may well pay you to look at six and working in python 3 than back converting to 2.7 for older installations.

See also here for both how to install python 3 libraries under python 3 with virtualevn and without, (modify version numbers as appropriate).

Update - the windows python launcher

Additionally, on Windows, when you install Python 3 you have the option to install the python launchers, (py.exe & pyw.exe), to the Windows directory. The py launcher has the following behaviour:

  • py Launch the python 3 interactive latest installed prompt (64 bit preferred)
  • py -2 Launch the python 2 interactive latest installed prompt (64 bit preferred)
  • py somescript *Check the shebang in the named file and launch it with python 2 (default if no shebang) or 3 as appropriate.
  • py -2 somescript launch the script under latest python 2 (64 bit preferred)
  • py -3 somescript launch the script under python 3 (64 bit preferred)
  • py -3.5 somescript launch the script under python 3.5 (64 bit preferred)
  • py -3.5-32 somescript launch the script under python 3.5 (32 bit)
  • the pyw launcher has the same behaviour without the console, like pythonw.

Associating .py files with py.exe and .pyw files with pyw.exe will result in the shebang line being honoured.

Additionally using py -3.5-32 -m pip install some_package will specifically install some_package to the 32 bit python 3.5 installation, etc.

Disclaimer: I am the author of some upcoming enhancements to the python launcher to extend the above options slightly.

like image 173
Steve Barnes Avatar answered Oct 24 '22 09:10

Steve Barnes