How to use a virtual environment - python

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.

Related

Azure function running with a python version different than virtual enviroment

I'm trying to debug my azure function with python in visual studio code. However, when I press F5, I continue to have this problem:
connect ECONNREFUSED 127.0.0.1:9091
So I noticed that my azure function is using python version 3.9.7 but my IDE is using python version 3.8.12 (virtual enviroment) and I have the libraries installed there.
Can someone help me solve this problem please?
I have created a new virtual environment and I tried to assign it to VS code but it does not worked. Also, I try running the function with func host start but I can't debug it.
You can install a python 3.9.7 in local and set that up as virtual environment.
You can do this with Pyenv. Check the below documentations to install pyenv and crate a virtual env with a specified python version.
https://github.com/pyenv/pyenv
https://realpython.com/intro-to-pyenv/
Alternatively, You can install a specific python version with apt. Check the below post and replace the python version with the one you wish to work on.
https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get
This way if you install python 3.9, then you can just do python3.9 -m venv venv and that will create a virtual environment called venv with python version 3.9.x.
You can reinstall your libraries there and try running your code again and it will run on 3.9.x.

How to create virtualenv with specific downloaded python version

I know there are some similar questions,but it is really hard for me to finish it.
I'm trying to create a virtualenv with python 3.7.7 in windows.
I have a downloaded python 3.7.7
C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64
Since I can built a virtualenv using:
python3 -m venv myenv
So I tried to modify it ,so that it can match specific python version:
python3 -m C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64\python.exe myenv
But it failed:
ModuleNotFoundError: No module named 'C:\\Users\\willi\\AppData\\Local\\Programs\\Python\\python-3')
Any friends can teach me how to build a virtualenv with python 3.7.7?
I think you haven't installed virtual environment in your local python
pip install virtualenv
and follow your steps. Stil, find the error. Try doing below methods, I think you will get your answer-
in command prompt
pip install virtualenv
go to the location, where you want to create your environment
cd location
virtualenv project_env_name
Now, you will find a python env in the desired location, Then go to scripts
cd project_env_name/scripts
activate
You will enter the environment you created. while leaving the environment, do
deactivate
To leave the environment.
This method works if you want to create the same python version environment as python version in your machine.
If you want to create an environment of the different version, you need to install the python of that version.

Multiple versions of Python in PATH

I've installed Python 3.7, and since installed python 3.8.
I've added both their folders and script folders to PATH, and made sure 3.8 is first as I'd like that to be default.
I see that the Python scripts folder has pip, pip3 and pip3.8 and the python 3.7 folder has the same (but with pip3.7 of course), so in cmd typing pip or pip3 will default to version 3.8 as I have that first in PATH.
This is great, as I can explicitly decide which pip version to run. However I don't know how to do to the same for Python. ie. run Python3.7 from cmd.
And things like Jupyter Notebooks only see a "Python 3" kernel and don't have an option for both.
How can I configure the PATH variables so I can specify which version of python3 to run?
What OS are you running? If you are running linux and used the system package panager to install python 3.8 you should be able to invoke python 3.8 by typing python3.8. Having multiple binaries named python3 in your PATH is problematic, and having python3 in your PATH point to python 3.8 instead of the system version (which is likely a lower version for your OS) will break your system's package manager. It is advisable to keep python3 in your PATH pointing to whatever the system defaults to, and use python3.8 to invoke python 3.8.
The python version that Jupyter sees will be the version from which you installed it. If you want to be able to use Jupyter with multiple python versions, create a virtual environment with your desired python version and install Jupyter in that environment. Once you activate that specific virtual env you will be sure that the jupyter command that you invoke will activate the currect python runtime.
I recommend you use pyenv a great tool for manage multiple python versions on the same system. Once installed you need to create a virtualenv, then activate the virtualenviroment and there you can install any libraries you want in a safe way.
By the way also come with an automatic installer pyenv-installer
Regards

problems creating virtualenv when installing two different versions of Python

I have installed Python 2.7, pip and virtualenv on my computer and I have created a virtual environment with this same version of Python. I installed some packages in this new virtual environment too.
Later, I decided to install Python 3.6 on the same computer to create a virtual environment that works with version 3.6. I downloaded Python 3.6 and installed it by selecting Add Python 3.6 to PATH and Install for All users and I selected the option to automatically install pip. As I had previously installed virtualenv, I have not had to reinstall it. Should I?
The problem is that, once installed, when creating a virtual environment I've got an error.
As I already had a version of Python installed I have specified that I want to use Python 3.6 in the virtual environment that I am creating:
virtualenv env2_py36 -p c: \ Python36 \ python.exe
The error already says that this usually happens when you install more than one version of Python on the same computer and warns that it is necessary to check the option Install for All Users to work (as I did) and also recommends placing the appropiate PythonXX.dll file in the Scripts folder of the virtual environment. However, I see that there is already a python36.dll file there.
Could anybody tell me what should I do to fix this problem?
On the other hand, I had previously created another virtual environment using Python 2.7. I use PyScripter to run my scripts but I don't know how to connect PyScripter to my virtual environment. It's still connected to the Python2.7's system installation.
This works for me:
virtualenv --python=python3.6 yourenvname
(3.6 is the current latest version of Python, but you can use whichever version you'd like.)

Python virtualenv switch to 3.5 from 2.7

How can I switch from virtualenv that uses python 2.7 to python virtualenv that uses python 3.5?
Migration from 2.x to 3.x Python has nothing to do with virtualenv. If you already built your project keeping in mind a version change, then it will not be hard.
You can just download the Python35 and install it. After that, just execute
virtualenv -p /path to your Python35 directory/python.exe name_of_env
to create a new virtual environment for Python3.5. You can imagine the new virtual environment as a fresh Python installation with no third packages.
Please note that virtualenv just creates a new environment inside your computer with the Python version you specified in the -p parameter (or if omitted the Python version that is specified in your Path).
You can then install the desired packages for your project after activating the new virtual environment (./name_of_env/Scripts/activate) by executing pip install package_name
Although keep in mind that version migration is not the simplest thing. Many things can go wrong and especially the packages version support. Most of the packages support Python 3.x but not all of them.

Categories