How to specify python version used to create Virtual Environment? - python

My Python virtual environments use python3.6 when I create them using virtualenv
~ $ virtualenv my_env
but I need to use python3.5 as 3.6 is not currently supported by Opencv3.
I've tried using the --python=<py_version> flag when creating a virtual environment but this doesn't work.
How do I specify the python (3.x) version to install using virtualenv for Mac and/or Linux?

Assuming that you have installed python3 or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example
$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)
$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)
And last
# Directly point to any version of python binary, this can be even another virtualenv's bin/python.
$ virtualenv -p /path/to/any/bin/python new_env

Alternatively, I think you could use the specific version of Python itself to create the virtual environment. That way, you'll know for sure it's the correct version:
$ python3.5 -m venv test35
$ ./test35/bin/python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build ) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Reference at https://docs.python.org/3.5/library/venv.html

As of version 3.3, python includes a package named venv. However that package doesn't provide the same functionalities as the traditional virtualenv package.
venv allows creating virtual environments only for the version of python it's installed for.
virtualenv allows creating virtual environments for different versions of python by providing the path to the binary.
Creating virtual envs for different versions of python:
So assuming one has python 2.7 and python 3.6 installed in /path/to/ and wants to create the virtual env named respectively env-py36 with python 3.6 and env-py27 with python 2.7
# create a virtual env with python3's venv :
/path/to/python36/bin/python3 -m venv /my/python-venvs/env-py36
. /my/python-venvs/env-py36/bin/activate
# we're now running python 3's "env-py36" virtual env, we want to install the "virtualenv" package
pip install virtualenv
deactivate
# now use virtualenv to create a virtual environment for python 2.7
/my/python-venvs/env-py36/bin/virtualenv --python=/path/to/python27/bin/python /my/python-venvs/env-py27
Using python 3.3+ venv
Python 3.3+ :
/path/to/python3/bin/python3 -m venv ENV_DIR
Python 3.3 to 3.5 (deprecated in 3.6+) :
/path/to/python3/bin/pyvenv ENV_DIR
Sources:
Creating Virtual Environments
Python 3.3 venv
Python virtualenv package

I working on all ubuntu and MacOS
Ubuntu : virtualenv -p python3.6 environment_file
Mac OS : virtualenv -p python3.6 environment_file
I think it be same

I had this issue (and came here) but under Windows. Python 3.9 was installed on one system but it had issues with code developed under 3.7. I wanted to use a virtual environment to downgrade to 3.7 to help debug the issue. Using Python Launcher for Windows:
py -3.7 -m venv my_env
in the python project folder did the trick for me.

Simple and direct solution:
Just see this video (https://www.youtube.com/watch?v=hC9FBQnOv6o) and follow the python setup download instructions of a particular python version and then use virtualenv <folder_name> -p /python.exe
This command is also shown in the video too.

In Linux:
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps:
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.

Using anaconda we can create a virtual environment called "py35_env" with Python 3.5 version by running:
conda create --name py35_env python=3.5

Related

Commands to run different python versions on venv at once CMD

I am currently running python 3.9 on my system. Due to the incompatibility of some of python libraries like numba on python 3.9 I will have to use previous versions. What cmd commands will I have to use to create a virtual environment so that I could run python 3.6 on cmd separate from python 3.9. I have a windows 10 64 bit system, I dont use conda or anything.
CMD Output:
C:\Users\maste>python -V
Python 3.9.1
In Windows you can utilize the Python Launcher for Windows. You can just install multiple Python versions from Python.org, and then use
py -3.6 -m venv venv
To create a virtual environment called "venv" (the last argument is the name). After that just activate your virtual environment and python will point to the Python 3.6 in your virtual environment.
Instead of the py launcher, you can also just use the full file path of the Python 3.6 (assuming Powershell, hence &):
& "C:\path\to\python 3.6\python.exe" -m venv venv
Lastly, you don't have to activate the virtual environment, if you don't want to. You can just run
<path-to-project>\venv\Scripts\python.exe myscript.py

Cannot activate correct Python in remote computer

I have moved a virtual environment onto a remote computer. I am trying to activate python 3.8 but not having much luck.
The virtual environment I'm using was created using venv. But I created it on my Mac and then moved it onto the target Linux computer. The following demonstrates that the Linux computer has python 3.8
kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate_this.py pip pip3.8 python3
activate.csh easy_install pip2 python python-config
activate.fish easy_install-2.7 pip2.7 python2 wheel
Activate.ps1 easy_install-3.8 pip3 python2.7
I activated the virtual environment with the following commands:
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
However, the following command shows that python 2.7 was activated
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python --version
Python 2.7.13
Further, when I ran one of my programs I got a syntax error that only python 2.7 would throw:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3 fix_mistakes.py
File "fix_mistakes.py", line 113
p = print
p (f"{round(c - b,0)} seconds")
SyntaxError: invalid syntax
Even when I run the command python3, it activated python 3.5 as demonstrated by the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
I even think that the computer is not even using my working environment but the default python interpreter due to the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ which python
/usr/bin/python
I would think that the intepreter would be in my virtual environment which would be:
/venv_byu/bin/python3
#####UPDATE
I was able to install venv on the linux but I'm still activating python 3.5.3
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ which python
/home/kylefoley/byu_corpus_small/venv_byu/bin/python
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ python --version
Python 3.5.3
Apparently the environment I downloaded did not have 3.8
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate.fish easy_install-3.5 pip3 python
activate.csh easy_install pip pip3.5 python3
Now, I just need to figure out how to get 3.8
Virtual environments are not portable. You should create a new virtual environment on the destination computer and populate it with the packages you need. Listing them with pip freeze on your local computer or manually enumerating them in requirements.txt are two common approaches.
The standard virtual environment shipped with Python actually hard-codes the path of the virtual environment, so you can't even rename the directory locally, much less copy it to a different directory structure on a different computer.
The activate command needs to be run with source for various reasons, but that also means that it is not very robust against failures. For example, it can throw an error or fail silently, but still update your prompt so that it appears as if the virtual environment was successfully activated.
To create a new virtual environment on a computer where you have Python 3.8 installed as /usr/local/bin/python3.8, you can run
/usr/local/bin/python3.8 -m venv venv_byu
You then need to activate this environment, and pip install or otherwise populate it with the libraries you need.
pip freeze will list the exact versions of all installed packages, so it is more precise in getting exactly the same version of everything. If you manually list packages in requirements.txt, you don't have to specify a precise version of anything, and you can leave out packages which are pulled in as dependencies of the packages you actually specifically depend on.

How do I downgrade my version of python from 3.7.5 to 3.6.5 on ubuntu

So currently, I have ubuntu 19. And it comes by default with python 3.7.5. I need to downgrade to 3.6.5.
EDIT:
I am using virtualenv
The following talks about upgrade from 3.6.7 to 3.7.0 but you can use the same process for downgrade. You should not change the system python unless you really know what you're doing
First Install Pyenv
Installlation Instructions are here
Look at Pyenv Options
$ pyenv
pyenv 1.2.14
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and deevlopment tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
Look at Python Versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
Install a new Python
$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0
If you run into an issue with _ctypes install libffi-dev library
Now look at the versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
Select 3.7.0 for local environment
$ pyenv local 3.7.0
See that the version changed
$ pyenv versions
system
3.6.7
* 3.7.0 (set by /home/taarimalta/.python-version)
$ python
Python 3.7.0 (default, Jan 1 2020, 10:52:57)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switch to a different folder
cd ../project2
pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
The python version may be different here depending on which python version you have set locally
Set pyenv version globally
This globally sets a python version for a user
pyenv global 3.7.0
Note that pyenv sets local version by adding a .python-version file
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
Note that pyenv knows the global version by looking at the ~/.pyenv/version file
cat ~/.pyenv/version
3.8.2
Install it from the pre-compiled Ubuntu deb package repos.
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
When prompted, pres ENTER to continue.
sudo apt install python3.6
Alternatively, you can easily create a virtual environment.
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps: (Checked August 2022)
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, and you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
You can also use virtualenv to use different Python environments.
If you type python and tab twice or so, you might see a number of Python versions available. I say this because when I type
python3.6 -V
I have
Python 3.6.9
available. My alias for python is 3.7.5
When you type python you are probably referring to an alias defined in .bashrc

Creating Python virtual environment in different version than installed on Windows

To run some script I need specific version of python (3.7.1).
On my PC I have python 3.7.3
For creating virtual environment I am using:
python -m venv virtual
Which gives me fully functional python but in version 3.7.3 and I need 3.7.1
How can this be achieved?
virtualenv can only create environments for versions of python installed on the machine.
You may install multiple versions in parallel on windows then select which one to use.
Just install Python 3.7.1. Assuming you are using virtualenv, create your environment as:
virtualenv -p /home/username/opt/python-3.7.1/bin/python Project_Name

change python executable to the one installed by brew on mac for virtualenv

My Mac comes installed with python 2.7.2 in /usr/bin. Yet for our project I need to use python 2.7.6. So I did a brew install python.
Now python 2.7.6 is installed in /usr/local/bin/python. Yet when I create a new virtualenv it still creates one with the 2.7.2 python version installed in /usr/bin.
How can I tell virtualenv to create a virtual environment with the 2.7.6 version?
Any Ideas?
Use this flag when creating the env:
$ virtualenv --help
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
...
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)

Categories