# Using different versions of Python together



## nosferatu (May 10, 2015)

Edit: Problem Solved

Hello,

I am setting up a system on VirtualBox in Windows 7 Ultimate. I have a little problem with usage of different Python versions together.

Well, I ported Xorg and it uses Python 2.x. But I will port a few things in which can work with Python 3.x and I want them to work with Python 3.x and I wanna use Python 3.x as default.

So, if I would just configure the make.conf and set the default version of Python as 3.x and build the new ports then (I won't build any other ports that uses Python 2) would they work with Python 3 and could there be a problem with Xorg?

Or is there be a better solution for this problem?

I've search the web for it but most of the answers were outdated or not satisfactory. I find a good one but not fully understand it:



> 20131003:
> AFFECTS: users of lang/python*and ports
> AUTHOR: mva@FreeBSD.org
> 
> ...



Can I do this in make.conf:


```
PYTHON_DEFAULT_VERSION="python3.3"
PYTHON2_DEFAULT_VERSION="python2.7"
PYTHON3_DEFAULT_VERSION0="python3.3"
```

Thank you.


----------



## getopt (May 10, 2015)

nosferatu said:


> Can I do this in make.conf:
> 
> 
> ```
> ...


See /usr/ports/Mk/Uses/python.mk for proper variables.
In most cases you are better off setting no default versions at all. 
Do it only when you have to do it for a good reason.
Do you want Python3 for coding?


----------



## nosferatu (May 10, 2015)

Yes I want Python 3 for coding.

So, for example Pyramid works with both Python 2 and Python 3. I didn't make any configurations about default Python version. If I install the Pyramid which one will it use?

Edit: Well, I understand it now . It just works with both. Sorry, I am a junior Computer Science student. I can sometimes ask this stupid questions and notice my mistake after :/. Damn, I've thought about it for all day.

Problem Solved...


----------



## getopt (May 10, 2015)

I do not know Pyramid, but you can select the Python interpreter in the shebang line.


----------



## GeorgeG (Feb 22, 2016)

I have newly installed FreeBSD and I am getting ready to install multiple versions of Python. I've read this thread and multiple sources of documentation about the ports/packages on freebsd.org, freshports.org, and python.org. By no means have I found and read everything relevant, but it seems clear that the recommended way to install multiple versions of Python has recently changed and maybe is still somewhat in transition. Figuring out which is the latest recommended method is proving a little dicey, so I'm asking here.

My intention is to get Python 2.2, 3.4, and 3.5 installed. I have that on Windows, both Win7 and Win10, with the Python launcher and environment variable PY_PYTHON=3. That makes the highest version of Python 3 installed (3.5) be the default, and shebang lines to be more specific. Some sample shebang lines, which all work, are:

```
#! python2
#! python2.7
#! python3
#! python3.4
#! python3.5
```
If I understand right, there will be no Python launcher per se on FreeBSD, the shebang lines need to be more explicit that the ones I am using on Windows (above), and there are separate packages/ports to install to set default Python versions.

Doing a "pkg search python" I've identified several that might be needed.
The Python versions themselves:
python27-2.7.11_1
python34-3.4.4_1
python35-3.5.1_1

And the meta ports to (presumably) make things point to default versions:
python2-2_3 (Meta port for default version of Python 2.)
python3-3_3 (Meta port for default version of Python 3.)
python-2.7_2 (Meta port default version of Python. Also found a note that says edit this one before installation to make it point to a version other than 2.7; I would want 3.5 and of course would want to change it in the future.)

The sample shebang lines would change to:

```
#! /usr/bin/python2
#! /usr/bin/python2.7
#! /usr/bin/python3
#! /usr/bin/python3.4
#! /usr/bin/python3.5
```
BTW, these modified shebang lines seem to work fine on Windows, so will be portable.

I also found this under "Notes from UPDATING" on page "http://www.freshports.org/lang/python/":
____
These upgrade notes are taken from /usr/ports/UPDATING
2013-10-03
Affects: users of lang/python* and ports
Author: mva@FreeBSD.org
Reason:
  The default versions of lang/python* have been changed to support the
  new DEFAULT_VERSIONS variable.

  PYTHON_DEFAULT_VERSION, PYTHON2_DEFAULT_VERSION and
  PYTHON3_DEFAULT_VERSION are deprecated. If you have set them in your
  make.conf, you should change them something like

  DEFAULT_VERSIONS=python=2.7 python2=2.7 python3=3.3​____

So, as far as what I need to (or should) do:
1) Modify shebang lines. Seems straightforward.
2) Install the three versions of Python (per packages/ports python27-2.7.11_1; python34-3.4.4_1; python35-3.5.1_1.
3) Do I need to install the packages/ports python2-2_3; python3-3_3; python-2.7_2 to set the defaults, and do I need to make changes to them prior to installation? Or are they the wrong packages to set the defaults?
4) Do I need to set the environment variables python, python2, and python3 separate from any installation steps?
5) Can I simply change the environment variables python, python2, and python3 in the future to reflect newer choices? Or do installations need to be re-done?

So far I haven't found a current summary of guidance on this, so if I missed one please point me to it.

Any help or clarifications would be appreciated!


----------



## chrbr (Feb 26, 2016)

Dear GeorgeG,

I have installed lang/python,lang/python2, lang/python27 and lang/python34. The only port I have not installed via dependency is lang/python27. This leads to the situation as below:

```
$ ls -l /usr/local/bin/python*
lrwxr-xr-x  1 root  wheel  7 24 Jan 11:01 /usr/local/bin/python -> python2
lrwxr-xr-x  1 root  wheel  14 24 Jan 11:01 /usr/local/bin/python-config -> python2-config
lrwxr-xr-x  1 root  wheel  9 24 Jan 10:21 /usr/local/bin/python2 -> python2.7
lrwxr-xr-x  1 root  wheel  16 24 Jan 10:21 /usr/local/bin/python2-config -> python2.7-config
-r-xr-xr-x  1 root  wheel  5408 23 Jan 09:19 /usr/local/bin/python2.7
-r-xr-xr-x  1 root  wheel  1769 23 Jan 09:19 /usr/local/bin/python2.7-config
-r-xr-xr-x  2 root  wheel  7280 31 Jan 20:20 /usr/local/bin/python3.4
lrwxr-xr-x  1 root  wheel  17 31 Jan 20:20 /usr/local/bin/python3.4-config -> python3.4m-config
-r-xr-xr-x  2 root  wheel  7280 31 Jan 20:20 /usr/local/bin/python3.4m
-r-xr-xr-x  1 root  wheel  3048 31 Jan 20:20 /usr/local/bin/python3.4m-config
```
You can see that the meta ports are established by links. By the way, in the current situation I would have had no need to install lang/python27 explicitly because it is already provided as dependency of other ports which require lang/python.

The conclusion is that you can install the meta ports for scripts where just the meta port is enough and portability is no issue if the exact version which is behind the dependency changes. When you install the versions you have mentioned above they will not be removed by `pkg autoremove`.  If you install the metaport an upgrade will pull in a newer version of the dependency port. This could be for example a lang/python28 by an update of lang/python2 or lang/python4 by an update of lang/python or so. I hope this answers your questions somehow.


----------



## GeorgeG (Feb 27, 2016)

chrbr,

Yes, thank you, it answers almost all of my questions. So far (after posting above) I had installed the Python 2.7, 3.4, and 3.5 packages. Then I manually set up symbolic links covering some of the above (python->python3, python3->python3.5, python2->python2.7). I didn't think about "-config" items, so I'll look for those. I had also come across the notion that Python debugger might be there as pdb and need its own sym links and a similar situation might exist with IDLE, but maybe that's part of what the -config items handle? IDLE I don't need on this particular system as I have not installed any GUI. If those -config items are not binaries maybe I can peak inside them and learn what they are doing.

One thing I had wondered is that I wanted 3.5 to be my default, and the only package (that I had located) set up 2.7 as the default, as you show above with plain python pointing to 2, and 2 pointing to 2.7. I had come across a reference somewhere to editing a make file of (one or more) of the meta packages before installing it. That's why I decided to just take a shot at installing the individual packages and setting up the sym links by hand. If there are not very many (and I keep notes) that's no problem. 

Really my desire to have the default be something different than what the meta sets up is pretty trivial. This is a learning platform for me so I might just leave what I have done in place (with any additional adjustments needed as mentioned above) and then, later on, pull it out and reinstall Python as you have above. 

BTW, did you install the meta port for Python 3 also (and it pulled in 3.4)? Or did you install 3.4 directly?

Thank you.


----------



## chrbr (Feb 27, 2016)

Dear GeorgeG,
the line below in /etc/make.conf


GeorgeG said:


> DEFAULT_VERSIONS=python=2.7 python2=2.7 python3=3.3


should take effect only if you build from source by using the ports system. I have just confirmed that in a jail where I use packages only. I do not know if something similar is possible with using the packages.


GeorgeG said:


> BTW, did you install the meta port for Python 3 also (and it pulled in 3.4)? Or did you install 3.4 directly?


I did not install lang/python3 or lang/python34 directly. lang/python34 has been pulled in as a dependency.


----------

