Downgrade Python version in virtual environment - python

I am always getting the same error regarding TensorFlow:
ModuleNotFoundError: No module named 'tensorflow.contrib'.
I am actually using Python version 3.9 but, reading online, it seems that version 3.7 is the last stable one that can work with TensorFlow version >2.0.
Unfortunately I have started my project in a venv with the wrong version of Python and I would like to downgrade it, how can I do that?

There's the venv --upgrade <ENV_DIR> command, but it's mainly intended to update between patch versions (e.g. from 3.9.15 to 3.9.16).
You'll need extra steps to update between different minor versions. Let me walk you through it.
Assuming you have both versions of Python installed:
$ python3.9 --version
Python 3.9.16
$ python3.7 --version
Python 3.7.16
And that your environment was created using version 3.9, and it's in a venv subdirectory:
$ . venv/bin/activate
$ python --version
Python 3.9.16
$ deactivate
Use these commands to downgrade from 3.9 to 3.7:
$ python3.7 -m venv --upgrade venv
$ cd venv/bin
$ ln -sf python3.7 python
$ ln -sf python3.7 python3
$ rm {python,pip}3.9
$ cd -
Here's the result:
$ . venv/bin/activate
$ python --version
Python 3.7.16

tf.contrib is deprecated from the latest version of TensorFlow 2.x and is replaced with TF Slim.
It is recommended to use the upgraded version of TensorFlow to have the benefit of the latest features and functionalities provided by TensorFlow.
However, To downgrade python
You need to uninstall the existing python version and re-install the required python version and set up your environment.
If you are using Anaconda IDE, then use the below command:
conda search python #to check available python version
conda install python=<version>
conda create --name <env_name> python=<python_version> # Which also creates virtual environment
activate <env_name>
Please check this link for more details.

Related

Connection between pip and python [duplicate]

I'm now currently using Python on Ubuntu 15.10.
But in my OS, I have many different python versions installed:
Python (2.7.9)
Python3 (3.4.3)
Python3.5
PyPy
So, it got messy with the versions of the packages in different environments. For example, if I run:
pip3 install django
But in fact, I cannot import django inside python3.5.
Is there any efficient way to call the correct version of pip?
Note:
Don't suggest that I use virtualenv, I know about it and am seeking another solution.
Finally I found the solution myself, see the Docs:
https://docs.python.org/3/installing/index.html?highlight=pip#work-with-multiple-versions-of-python-installed-in-parallel
Just call:
pythonXX -m pip install SomePackage
That would work separately for each version of installed python.
Also, according to the docs, if we want to do the same thing in windows, the command is a bit different:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
How about using pyenv?
You can switch the version.
$ pyenv install 2.7.X
$ pyenv install 3.5.X
$ pyenv local 2.7.X
$ pyenv global 3.5.X
This solution worked for me:
sudo python2.7 -m pip install [package name]
Why not using anaconda?
If you use conda, you can easily create/manage virtual env. For example, if you have root env python 3.4 and py27 env for python 2.7, you can easily switch between them use command source activate [env]
source activate py27
conda install SomePackage

wsl ubuntu 20.04 -> with python3.9 and without python3.8

Want to use python3.9 with actual pip and venv. And remove python3.8.
I've installed python3.9 like that. Then pip. And python3.9-venv using apt.
Problem is pip and venv use distutils. Whish downloads via python3-distutils (python3.9-distutils is virtual package which refers to python3-distutils) and drags python3.8 with it.
I tried to remove python3.8 with all that methods one by one. But each time distutils removes along with python3.8. I read that dialogue. And I'm not quite sure but it seems there is no distutils outside of python3.8 package.
So am I nailed forever with python3.8 or there is solution to remove it safely? Or somehow extract distutils and tie it with python3.9?
Do not remove the system Python
The Ubuntu 20.04 system needs Python 3.8 for its own functionality. The system Python, in this case Python 3.8, should not be removed, because that can make the system instable.
Python 3.8 does not need to be removed to use Python 3.9.
More info here: https://unix.stackexchange.com/questions/652299/changing-pythons-default-version-breaks-ubuntu-20-04
No need for python3.9-distutils
python3-distutils works for both Python 3.8 and Python 3.9, no need for python3.9-distutils.
Source: https://github.com/deadsnakes/issues/issues/150#issuecomment-761180428
Create a venv virtual environment with Python 3.9
yourname#machine:~$ python3.9 -m venv /home/yourname/.venvs/my-venv-name
Activate the virtual environment:
yourname#machine:~$ source /home/yourname/.venvs/my-venv-name/bin/activate
Check the python version, it should be 3.9:
(my-venv-name) yourname#machine:~$ python -V
Python 3.9.9
Check the pip version within the venv, it is probably different than the system pip version:
(my-venv-name) yourname#machine:~$ pip3 --version
pip 21.2.4 from /home/yourname/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)
Deactivate the virtual environment:
(my-venv-name) yourname#machine:~$ deactivate
Check the system pip version, outside any venv:
yourname#machine:~$ pip3 --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

install older virtual environment compatible with python 3.7.4?

Is it possible to install an older virtual environment compatible with python 3.7.4? I want to use python 3.7.4, but when I install a virtual environment with the command:
python3 -m pip install pipenv
it installs the latest virtual environment, but when I go to activate the virtual environment, I get the following error message in my mac terminal:
Warning: Your Pipfile requires python_version 3.8, but you are using 3.7.4 (/Users/myHomepage/.local/share/v/t/bin/python).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
Launching subshell in virtual environment…
I am using Visual Studio Code. Any ideas? thnx.
Also can some1 tell me how different Python 3.7.4 is to Python 3.8? I'm following along with a tutorial wherein he says it is highly recommended to use Python 3.7.4 and not 3.8, but how different are they? thnx
i suggest you use pyenv for managing python versions on mac it's easy to use and available in brew
for installing it in terminal just do:
$ brew update
$ brew install pyenv
and if you are using bash
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
finally restart your shell and you can install any python version with the currect pip version using pyenv
pyenv install 3.8.5
pyenv global 3.8.5

Migrating an env to a new python version

I have a python app build on python 3.4
Due to an OS update I have to update this app to python 3.5.
First step I did was this to create new python version path in env
python3 -m venv --upgrade ENV_DIR
But how can I reinstall the needed packages from requirements.txt into this new 3.5 path?
When I do
pip install -r requirements.txt
it says all packages are already installed.
What am I doing wrong? Can someone help me?
Thank you
You can also use pyenv for multiple python versions.
Install pyenv
check available python versions using pyenv.
pyenv install --list | grep " 3.[45]"
Install the version you want. Here you will need 3.4
pyenv install 3.4(Any version you want)
Find available versions
pyenv versions
Go to local folders where your project is there. Set python 3.4 version
pyeve local 3.4
Check python version. It would be 3.4
python --version
Create virtual Environment
python -m venv venv
Install all packages using
pip install -r requirements.txt

How do I change PIP to use Python 2.7

I am on AWS Ec2 Amazon AMI. Trying to install virtualenv but PIP is set to use Python2.6
# pip -V && virtualenv --version
pip 9.0.1 from /usr/local/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg (python 2.6)
bash: /usr/bin/virtualenv: No such file or directory
# python -V
Python 2.7.12
I tried uninstalling virtualenv and reinstalling it but no luck.
#pip install virtualenv
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Requirement already satisfied: virtualenv in /usr/local/lib/python2.6/site-packages
Here is the install directory:
# which pip
/usr/sbin/pip
# which python
/usr/bin/python
1.python2.7 -m pip install virtualenv
Reference Python official document
2.You can Install virtualenv for any python interpreter and use for a different python interpreter like:-
sudo pip3 install virtualenv
and then if you want to use python2.7 python interpreter then run following command to make virtualenv:-
virtualenv --python=/usr/bin/python2.7 virtualenv_name
NOTE :- python2.7 interpreter has to be present in /usr/bin/* folder
For more on above command see this answer

Categories