Mac OSX: Switch to Python 2.7.3 - python

I've installed the Mac OSX Python 2.7.3, and tried switching to the new version:
$ python2.7 -V
Python 2.7.3 # It was installed
$ python -V
Python 2.6.1 # Default is 2.6
$ export VERSIONER_PYTHON_VERSION=2.5
$ python -V
Python 2.5.4 # I can switch to 2.5
$ export VERSIONER_PYTHON_VERSION=2.6
$ python -V
Python 2.6.1 # And 2.6
But:
$ export VERSIONER_PYTHON_VERSION=2.7
$ python -V
python: VERSIONER_PYTHON_VERSION environment variable error (ignored)
Python 2.6.1
Can't switch to 2.7! Why is that?

The python.org install will put a python executable in /usr/local/bin . Apple's python is in /usr/bin
To call the python.org python you can use its full path /usr/local/bin/python or change your path to put /usr/local/bin before /usr/bin. You will still be able to call Apple's python by using its full path /usr/bin/python
Some python installs e.g. Macports provide a tool that provides symbolic links to various python installs and allows you to switch between them.

I don't think we can switch the python version on the Mac OS X without resorting to something like MacPorts or ActivePython.

I think it's because VERSIONER_PYTHON_VERSION is specific to python packages built by apple.

Related

Python Multiple version installation in Linux rocky

I am new to Python and Linux env, so a little confused.
I want to find where my Python3.9 is installed, so that I can update the PATH..
Running python --version or python3 --version give me Python 3.6.8
I want to install Python 3.9, so when I run sudo dnf install -y python39 it gives me
Package python39-3.9.12-1ep1.el8.x86_64 is already installed.
ls /usr/bin/python* doesn't show 3.9
Output of above command - /usr/bin/python /usr/bin/python3 /usr/bin/python3.6
/usr/bin/python3.6m
Also tried alternatives --config python but it shows only 3.6
-----------------------------------------------
* 1 /usr/libexec/no-python
+ 2 /usr/bin/python3
which python3.6 gives /usr/bin/python3.6
which python3.9 gives /usr/bin/which: no python3.9 in....
rpm -ql python39-3.9.12-1ep1.el8.x86_64 gives me
/etc/gdbinit.d/python39.gdb. /opt/python3.9.
/opt/python3.9/bin
I could be missing something obvious, if someone can point me in the right direction it will be awesome.
what happens when you enter the following command: python3 --version and is it any different compared to running python --version?
I am not sure what is your actual requirement is. But if you have already installed python 3.9 in your machine, then you can have this and all previous versions applied to different projects. This is selected when you create virtual environments. For example if you want to run a project that built on 3.6, then you can select python interpreter 3.6 at the time of creation.
So the virtual machine I was working was built using vagrant, it had python 3.6 in usr/bin and python 3.9 in opt/python3.9. Newbie like me who wanna understand what /opt is - https://www.baeldung.com/linux/opt-directory
To search any package used command - rpm -ql python39-3.9.12-1ep1.el8.x86_64
Once package is found add it to path as symlinking binaries can be confusing
vim ~/.bashrc
PATH=/opt/python3.9/bin

How to test a library against different Python *patch* versions?

I'm writing a library and want to test against different Python patch versions, like 3.7.1, 3.7.2, etc
I've been using tox for a long time, however, according to this answer, it doesn't really support this kind of usage.
Any suggestions?
For a one of check against 3.8.1 (assuming your python3.8 points to 3.8.2) you can use the discover flag
tox --discover /path/to/python3.8.1 -e py38
If you want to define an environment that always uses 3.8.1 you can do that by defining a new tox environment and setting basepython as python3.8.1.
[testenv:py381]
basepython = python3.8.1
[testenv:py382]
basepython = python3.8.2
Probably the least troublesome (but tedious) way is to install different versions of Python sequentially in a jail or VM and then test your code on it.
If you install Python from source on a UNIX-like system, you could try installing them side by side using different prefixes (eg /opt/patch1, opt/patch2 etc.)
And then expliticly run your test with the correct python like /opt/patch1/bin/python3. One caveat; I'm not sure if the Python executable would find the correct shared library in this case.
The ms-windows installer lets you pick an install location. If you instruct it not to put Python in the PATH and not set up file associations and the like, that might also work. You would also have to explicitly invoke the correct Python with the full path.
I would go Docker and run it container.
$ docker run -it --rm -w /opt -v "$PWD:/opt" python:3.4.2 python <script.py>
-it - interactive mode
--rm - remove container after the run
-w - working directory inside container
-v - map directory $PWD from host to /opt inside container
<container> - python:3.4.2
<command> - python script.py
You can see what images are available with the command:
$ curl -s https://registry.hub.docker.com/v1/repositories/python/tags | \
jq -r .[].name | grep "^[23][.0-9]*$" | sort -V
2
2.7
2.7.7
2.7.8
2.7.9
2.7.10
2.7.11
2.7.12
2.7.13
2.7.14
2.7.15
2.7.16
3
3.2
3.2.6
3.3
3.3.5
3.3.6
3.3.7
3.4
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.4.9
3.4.10
3.5
3.5.0
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.6
3.5.7
3.6
3.6.0
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.6.8
3.6.9
3.7
3.7.0
3.7.1
3.7.2
3.7.3
3.7.4
grep filters out beta and alpha versions, so if you need them - just remove the grep.
If you need Python version that is not present in the list, you can build docker image with custom Python.
take eg Alpine linux (it's really small)
https://github.com/docker-library/python/blob/f82205cde8f0a5ffa276103a50d843edced67757/3.7/alpine3.10/Dockerfile

ImportError: No module named psutil while using robotframework

I'm using robotframework for automation. I'm trying to use a method from my python library which needs psutil. The problem is when I execute from the robotframwork it gives error ImportError: No module named psutil but when I execute it with Pycharm I don't get any error. I have installed psutil using pip install psutil . I searched a lot on web but couldn't find any reason for this issue.
My experience with this error was that I had multiple versions of python installed on my ubuntu server 16.04LTS.
My PATH resolved to /usr/local/python -> python 2.7.10 meaning typing python into the commandline I got 2.7.10, but the standard path /usr/bin/python linked to python 2.7.12. The psutil module and others were only installed for version 2.7.10
My resolution was to re-symlink /usr/bin/python to point to my 2.7.10 version:
# Find which python your PATH is pointing at and the version
$ which python
# Returns
/usr/local/bin/python
$ /usr/local/bin/python --version
# Returns
Python 2.7.10
$ /usr/bin/python --version
# Returns
Python 2.7.12
Unlink and relink to correct python version in /usr/bin/python
$ cd /usr/bin
$ sudo unlink python
$ sudo ln -s /usr/local/bin/python python
# check python version points correctly
$ /usr/bin/python --version
# Now correctly returns
Python 2.7.10
P.s. Bear in mind it's still worth checking all your modules are correctly installed now that your environment is pointing at the 1 python version
Could you try setting the PYTHONPATH in your environment or by passing it as an option.
This person had a different issue, but the fix I think will be the same:
setup pythonpath before starting test suite
Cheers,
K

Use updated version of Python on Linux

I have Python 3.4 installed on my Linux computer.
sudo apt-get install python3.4
However, when I run python -V, it shows that Python 2.7.6 is being used.
How do I tell the system to use the updated version of Python?
The answer to this question for a windows computer is at How to update version of Python?, but I couldn't find an answer for Linux.
On Linux, installations of python3 installed by the package manager (e.g. apt) can be called as python3. You might need to specify the version - e.g. python3.5 if the package manager has installed more than one, or you've compiled your own installations from source.
you can specify the version in the shebang.
write #!/usr/bin/env python3.
when run via ./my_script.py it will run in python3.
otherwise run it via python3 my_script.py.
if you just want to start an interactive python shell start it with python3
you also can be more specific with the version. just replace python3 with python3.4 (if installed)
In ubuntu various python executable are places under /usr/bin/ and might look like
/usr/bin/python
/usr/bin/python3.2
/usr/bin/python3.4
etc. so when you execute a command python -v it looks for a file with that name in that location. so to choose your version specify it like python3.4 -v
you can replace the simlink /usr/lib/python with /usr/bin/python3.4 to make that "default"

How to check all versions of python installed on osx and centos

I just started setting up a centos server today and noticed that the default version of python on centos is set to 2.6.6. I want to use python 2.7 instead. I googled around and found that 2.6.6 is used by system tools such as YUM so I should not tamper with it. Then I opened up a terminal on my mac and found that I had python 2.6.8 and 2.7.5 and 3.3.3 installed. Sorry for the long story. In short I just want to know how to lookup all the version of python installed on centos so I don't accidentally install it twice.
The more easy way its by executing the next command:
ls -ls /usr/bin/python*
Output look like this:
/usr/bin/python /usr/bin/python2.7 /usr/bin/pythonw
/usr/bin/python-config /usr/bin/python2.7-config /usr/bin/pythonw2.7
we can directly use this to see all the pythons installed both by current user and the root by the following:
whereis python
Find out which version of Python is installed by issuing the command
python --version:
$ python --version
Python 2.7.10
If you see something like this, Python 2.7 is your default version. You can also see if you have Python 3 installed:
$ python3 --version
Python 3.7.2
If you also want to know the path where it is installed, you can issue the command "which" with python and python3:
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
Here is a cleaner way to show them (technically without symbolic links). This includes python2 and python3 installs:
ls -1 /usr/bin/python* | grep '.*[2-3]\(.[0-9]\+\)\?$'
Where grep filters the output of ls that that has that numeric pattern at the end ($).
Or using find:
find /usr/bin/python* ! -type l
Which shows all the different (!) of symbolic link type (-type l).
Use, yum list installed command to find the packages you installed.
COMMAND: python --version && python3 --version
OUTPUT:
Python 2.7.10
Python 3.7.1
ALIAS COMMAND: pyver
OUTPUT:
Python 2.7.10
Python 3.7.1
You can make an alias like "pyver" in your .bashrc file or else using a text accelerator like AText maybe.
As someone mentioned in a comment, you can use which python if it is supported by CentOS. Another command that could work is whereis python. In the event neither of these work, you can start the Python interpreter, and it will show you the version, or you could look in /usr/bin for the Python files (python, python3 etc).
It depends on your default version of python setup. You can query by Python Version:
python3 --version //to check which version of python3 is installed on your computer
python2 --version // to check which version of python2 is installed on your computer
python --version // it shows your default Python installed version.
compgen -c python | grep -P '^python\d'
This lists some other python things too, But hey, You can identify all python versions among them.
Sift through the output of this script.
sudo find / -name 'python*' -type f -exec du -h {} + | sort -r -h ~/Documents/python_locations.txt
ls -l /usr/bin/python* & ls -l /usr/local/bin/python*
I would add to #nurealam siddiq answer,
python --version // it shows your default Python installed version.
python2 --version // to check which version of python2 is installed
python3 --version //to check which version of python3 is installed
python3.X --version // to further check which python3.X is installed
To check python versions installed in your OS you can run the below commands:-
python2 -version
python3 -version

Categories