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

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.

Related

How to change python3 version on mac to 3.10.10

I am currently running python 3.9.13 on my mac. I wanted to update my version to 3.10.10
I tried running
brew install python
However it says that "python 3.10.10 is already installed"!
When i run
python3 --version
in the terminal it says that i am still on "python 3.9.13"
So my question is, how do i change the python version from 3.9.13 to 3.10.10? I already deleted python 3.9 from my applications and python 3.10 is the only one that is still there.
I also tried to install python 3.10.10 from the website and installing it. However it does not work. Python 3.10.10 is being installed successfully but the version is still the same when i check it.
Python 3.10.10 is already installed along with Python 3.9.13. Your path is probably pointing to 3.9.13 and that's why you're getting that Python version.
Try modifying your path variable to point to brew's Python installation, or (better yet) make use of a virtual environment.
a) Telling what Python version your PATH variable is pointing to:
$ which python
Will tell you what's the actual executable's path. That way, you are going to see exactly where you main python resides.
b) If you want to create a virtual environment with brew's python try something like:
$ /usr/local/Cellar/python#3.10/3.10.1/bin/python3 -m venv py310
$ source venv/bin/activate
$ python
> # you should be inside a 3.10.1 envornamente
> CTRL+D
$ pip install requests
you can use pyenv to work with multiple python environments
things to do:
install pyenv : brew install pyenv
install particular python: pyenv install 3.10.10
set python3.10.10 to gloabal python env: pyenv global 3.10.10
and can start using python 3.10.10 version
Just delete the current python installation on your device and download the version you want from the offical website. That is the easiest way and the most suitable one for a beginner.

Conda unable to create different Python version environment

I am trying to create a new conda Python 2.7 environment.
For this purpose I am typing this in terminal:
conda create -n my_env_name python==2.7
After activating created environment (conda activate my_env_name) and checking Python version (python --version) I am getting Python 3.10.2
No matter which Python version I am trying to use in the new environment I am always getting Python 3.10.2 answer when checking the Python version.
Any idea what is wrong and how to solve this?
(I am working on iMAC, Chip Apple M1, macOS Monterey 12.1)
(After doing the same on my old machine everything works fine and after checking the Python version in a newly created environment I am getting Python 2.7.18.)
Here are some additional info.
When env is activated commands:
which python gives- /opt/local/bin/python
type python gives- python is /opt/local/bin/python
echo $PATH gives- /opt/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/uros/Desktop/iraf-main/unix/hlib/ecl.sh://Users/uros/.iraf/bin:/opt/anaconda3/envs/py27/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin
When env is not activated commands:
which python gives- /opt/local/bin/python
type python gives- python is /opt/local/bin/python
echo $PATH gives- /opt/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/uros/Desktop/iraf-main/unix/hlib/ecl.sh://Users/uros/.iraf/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin
After running conda list output is this:
I had the same problem with my M1 Mac.
As it turns out when working with python versions below 3.8 according to this post here there is no support for running osx-arm64. They go into the details on how to fix this.
You can also follow this blog post from Danny Cunningham How to Manage Conda Environments on an Apple Silicon M1 Mac
Manage both ARM64 and x86 Python environments using conda
in your case it should be something like:
CONDA_SUBDIR=osx-64 conda create -n my_env_27_x86 python=2.7
I hope this helps,
TL;DR With conda3, specify the Python 2.7 version fully, e.g., 2.7.18.
When I tried to reproduce your command in my own conda3 environment, I got this error:
PackagesNotFoundError: The following packages are not available from current channels:
- python==2.7
However, when I specified the version of Python 2.7 fully, it worked for me:
conda create -n p27 python==2.7.18
conda activate p27
python --version
shows that Python 2.7.18 is the default Python in that environment.
Alternative: use conda2
With Anaconda3/Miniconda3, the default Python will be Python 3.x, whereas with Aanconda2/Miniconda2, the default Python would be Python 2.7.x. On your old machine, you might have had conda2 installed, which would explain why it worked.
Thanks to #FlyingTeller for pointing out conda2 is not necessary, though.

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

python versions management: always results python 2.7 in console

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.)

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