Creating Python virtual environment in different version than installed on Windows - python

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

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

How to install virtual environment with python 3 without upgrading existing version of python on my OSX which is 2.7

Is it possible to have a virtual environment setup with python 3 if my OS has python 2.7 installed, I am using "$ virtualenv python3env -p python3" but its trying to look for a PATH for python3 on my system. Is there a work around for this problem?
install python3, DON'T delete your python2
after installing python3, make sure the to add the directory to PATH
change the binary(python,pythonw) inside python3 to python3/pythonw3
try to setup virtual environment using the same command in your post and you should now created a python3 virtual environment

How to specify python version used to create Virtual Environment?

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

How to use a virtual environment

Using Python I require both python 2.7 and python 3.5 for different packages. I am trying to install the following package NepidemiX. I get an error when I do this as I have a newer version of python installed.
To combat this I am trying to create a virtual environment. To do this I am using the virtualenv package.
I have created and activated this and am now faced with
(my_project)Your-Computer:your_project UserName$)
In my terminal.
How do I now proceed to install my package from here? Do I need to install python 2.7 in this environment first, or do I simply copy the desired package into the environment ... ?
Please could you instruct me how to correctly set this up?
Many thanks!
Virtual environment is only for libraries. It uses python versions installed on your computer. You can specify the version of python by using the -p attribute while creating the environment, for ex. virtualenv -p python3 env creates a python 3 enviroment (provided you have it installed in your computer and on the PATH). Check this answer.
After you activate the environment (source /env/bin/activate), just pip install libraries, and the environment takes care of installing the correct version.

Virtual environment starts a different version of python on a different machine

I've been using virtual environment successfully for some time, but this is the first time i run into such a problem.
In my virtual environment i have Python 3.5 + Django package + number of other packages. I successfully run my Django app using Python 3.5 from virtual environment on machine A.
On machine B, after I source env/bin/activate my virtual environment. python command would start Python 2.7 + no Django package present.
How do i investigate this and make venv behave in same way on both machines?
There are two options to get the same Python version in a virtual environment.
Check if there's Python 3.5 installed on your machine by typing
python3 -V
into the command line. If it says 3.5.x you can go on and create a virtual environment with
pyvenv /path/to/your/new/venv
You may install your packages (Django, etc.):
on machine A
pip freeze > requirements.txt
transfer requirements.txt to machine B and do
source /path/to/your/new/venv/bin/activate
pip install -r requirements.txt
If the python version that you've checked in 1. is not 3.5.x you have to compile this version by yourself. Check this question Use different Python version with virtualenv

Categories