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
Related
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.
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
I have renamed python.exe to python37.exe to avoid conflict with other versions. It works for running python, but if I run pip37.exe (located in /Scripts) I get the following error:
Fatal error in launcher: Unable to create process using '"c:\python37-32\python.exe" "C:\Python37-32\Scripts\pip37.exe"
Is there a way to keep python.exe renamed to python37.exe, but keep all python tools working?
This sounds like A BAD IDEA.
There are tools designed to help you manage exactly this sort of thing. The best of which imho is pyenv: https://github.com/pyenv/pyenv
It's quite simple to install. It takes a bit of getting used to – wrapping your head around virtual environments – but it makes it all so much easier to work with ultimately.
E.g. On my system I have the following versions of python:
pyenv versions
system
2.7.10
* 3.5.6 (set by /Users/.pyenv/version)
3.5.6/envs/core4
3.6.4
3.6.4/envs/core5
core4
core5
The one with the asterisk is currently the global version, which will be the one used from any default shell. I can change that using pyenv global 3.6.4 for example. I can also create virtual environments. E.g. core4 and core5 are virtual environments I created for specific projects. Each of these will have their own different libraries installed by pip install and differing python versions. You can activate a virtualenv for a given shell session e.g. pyenv activate core5.
And if you're thinking "what on earth does this have to do with Windows", look here: https://duckduckgo.com/?q=Windows+Subsystem+for+Linux&atb=v93-1__&ia=web and here: http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows/
On Windows Python installs PyLauncher. You don't need virtual environments or renaming tricks. py.exe is in the standard Windows path, has command line switches to pick the Python version to use, and enables using "shebangs" to specify what version of Python to run for scripts:
py script.py # Run the latest Python installed (or specified by PY_PYTHON environment variable).
py -2 script.py # Run the latest Python 2 version installed.
py -3 script.py # Run the latest Python 3 version installed.
py -2.7 script.py # Run the specific Python version.
py -2.7-32 script.py # Run the 32-bit specific Python version.
py -0 # List Python versions installed.
Scripts can use shebangs similar to Linux:
#!python2
#!python3
#!python2.7
#!python2.7-32
To run pip with a specific version:
py -2.7 -m pip install ...
If you need still would like a virtual environment with a specific Python version, you can specify the version (e.g. -3) and use:
py -3 -m venv <my_env_name> # to create an virtual environment
<my_env_name>/scripts/activate # to activate that environment
Activation adds the virtual environment to the path, so python (not py) will run in that environment. the Scripts directory in that environment will also be added to the path, so pip can be run directly as well to install packages in that environment.
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
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