python versions management: always results python 2.7 in console - python

Several versions of python are installed on my Mac.
Usually, I am using Anaconda with Python 2.7. Nowadays I decide to try Python 3.6
Environment parameters:
which python
#/Users/User/anaconda/bin/python
which python3.6
#/usr/local/bin/python3.6
echo $PATH
/Users/User/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/User/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
The problem is:
~ User$ /usr/local/bin/python3.6 --version
Python 2.7.14 :: Anaconda, Inc.
How can I to overcome this trouble?

The output from the second command where running python3.6 --version prints out a Python 2.7.14 string doesn't make sense to me. You might try removing the Anaconda directories from your PATH environment variable and see if that resolves it.
The simplest way to manage and install multiple Python versions is through pyenv.
It allows you to install new versions with pyenv install <version> for dozens of Python versions including CPython as well as alternative interpreters like PyPy.
I would recommend you try installing pyenv:
$ brew install pyenv
Then install Python 3.6.5 via it:
$ pyenv install 3.6.5
You can set this version as your system-wide default Python version as well:
$ pyenv global 3.6.5
Then in projects where you want the python command to point to a Python 2.7 shim, you can set the local version in that directory with:
$ pyenv local 2.7.14
With these commands, you can just run python in any one of your project directories (after you've set a custom override version if desired), and not have to worry about calling python3.6 in some places, python2.7 in others, etc.
In a more advanced setup, you can also provide multiple Python versions if you have a project that needs both Python 2 and Python 3 together, for example, in a shell session:
$ pyenv shell 3.6.5 2.7.14
$ pyenv version
3.6.5 (set by PYENV_VERSION environment variable)
2.7.14 (set by PYENV_VERSION environment variable)
$ python --version
Python 3.6.5
$ python2 --version
Python 2.7.14
$ python3 --version
Python 3.6.5
(In this case whichever version you set first is the one that python points to by default.)

Related

Why there are two python versions in a virtual environment?

I have created a virtual environment using anaconda in VS. When the environment is active. I check the version using python --version, it gives the following output Python 3.9.9, whereas when I use which python and check the version from the path /usr/bin/python --version I get a different version Python 2.7.18. Why is that happening, and which version does the environment use?
Once your virtual environment is activated the python command will use the python version from your venv (located in path/to/.venv/bin/python).
which python and /usr/bin/python forces the use of the python version installed in /usr/bin/python which in your case seems to be version 2.7.18. If you want to change your default Python version (the one thats used with python without a venv being active) you can use sudo ln -s /usr/bin/python /usr/bin/python3.9.

How to get the python command to go back to using python 2.7 Ubuntu?

So I made a mistake and routed the python command to point to python 3.6.9 and now certain programs (namely ROS packages) are having issues. I'm on Ubuntu 18.04. I see lots of tutorials telling people how to alias python to python3 but not how to fix this issue. Also I tried to unalias python, but that did nothing.
So now when I run python --version OR python3 --version I see python 3.6.9
And when I run pip --version OR pip3 --version I see pip 20.1.1 from /home/me/.local/lib/python3.6/site-packages/pip (python 3.6)
You can install an isolated Python 2.7 in your system and actually have several versions of Python. One of the easiest ways is pyenv
You can do it like this:
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
Follow the instructions, you may need to reload your env (re-log in into the console).
Then install and set Python 2.7 as a default Python:
pyenv global 2.7.18

Ubuntu won't recognize updated version of Python with pyenv?

I am trying to update from Python 2.7.17 to 3.8.1 so that I can run pip install pandas. I have installed pyenv, followed the instructions to add pyenv to my path, and installed 3.8.1. But every time I run python, it will not use version 3.8.1.
Running pyenv version returns 3.8.1, running python -V returns 2.7.17. Similarly, running python runs 2.7.17, and when I type python3 it runs Python 3.7.6?! But if I run pyenv global 3.7.6 the terminal tells me that 3.7.6 isn't installed!
I have also run pyenv global 3.8.1 in attempt to switch from 2.7.17 so 3.8.1, but still defaults to 2.7.17 when I type python.
I am using Ubuntu on Windows 10, if that helps.
Any help is greatly appreciated -- I am very very confused. Thanks in advance.
You could just use
pyenv shell 3.8.1
to set the PYENV_VERSION environment variable to the desired 3.8.1 version. But, I recommend you to use a virtual env:
pyenv virtualenv 3.8.1 my_test_env
and the activate it with:
pyenv activate my_test_env
For more info, this guide is super useful.

Flake8 uses system python instead of virtualenv

My flake8 is giving different outputs to a colleague's, and the difference seems to be in the python version:
(hydro2) andrew#xxx $ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 2.7.12 on Linux
Whereas on his machine it says CPython 3.5.0 or so.
I'm running the flake8 installed inside a python 3.5 virtualenv, and everything seems to point to the right place:
(hydro2) andrew#xxx $ which python
/home/andrew/virtualenvs/hydro2/bin/python
(hydro2) andrew#xxx $ python --version
Python 3.5.2
(hydro2) andrew#xxx $ which flake8
/home/andrew/virtualenvs/hydro2/bin/flake8
So I'm just confused as to why it's using CPython 2.7.12 (which is my OS's default) instead of the virtualenv's version?
I have tried uninstalling and reinstalling flake8 in the virtualenv.
To run a command line tool from a virtual environment you use python -m
Therefore, to run flake8 from your virtual environment:
python -m flake8
This assumes that you activated your virtual environment.

Upgrading Python on Ubuntu 15.10

I'm trying to upgrade my python to 2.7.11 on Ubuntu 15.10, by following the guides here
http://tecadmin.net/install-python-2-7-on-ubuntu-and-linuxmint/
http://mbless.de/blog/2016/01/09/upgrade-to-python-2711-on-ubuntu-1404-lts.html
But after, when I try and reopen terminal, and type in python it still shows the version is 2.7.10
Does anyone know why this is the case?
The post you've linked says explicitly in the first sentence: "you should not touch the Python version of the system." i.e., /usr/bin/python should remain the same and therefore if /usr/bin is earlier in your $PATH envvar than the path to the newly installed python version then python invokes /usr/bin/python and you see the old version.
To install/manage multiple minor python versions, you could use pythonz or similar tools (such as pyenv):
$ pythonz install 2.7.11 # to install 2.7.11 version
$ $(pythonz locate 2.7.11) # to start the corresponding version
You could create a virtualenv using the desired python version (using virtualenvwrapper's command):
$ mkvirtualenv -p $(pythonz locate 2.7.11) py2.7.11
python will refer to 2.7.11 version inside the virtual environment.

Categories

Resources