Create your own VE with Python
Soumyabrata Roy
Generative AI | Machine Learning | AI Engineer @ Deloitte ???? Ex-Cognizant ?? YouTuber @ DataDrivenDecision ?? Writer @ Medium
VE (virtual environment) could really help you in executing your products or services with different requirements. You joined a new job where they are still using Python 2.0. If you know how to create a virtual environment, it will be much easier for you to do things straight away.
Creating a virtual environment is pretty easy and straight forward. You can create a virtual environment easily using the library called virtualenv. To install the library go to your terminal and type
pip install virtualenv
To create a virtual environment in Python3, first, choose the folder where you would like to create the virtual environment. Then using your command prompt, go to your directory (folder) and write virtualenv "name of the environment" as an example:
virtualenv environment name
It will take some time and create a virtual environment for you. For example, if I would like to create a virtual environment called Venv at the location C:\Users\Sam\Environments, in the command prompt I will write,
<base> C:\Users\Sam\Environments\virtualenv Venv
and it will create a virtual environment named "Venv" for me.
Now you have created the virtual environment successfully, how to activate it? To activate the virtual environment, go to your Venv directory, look for Script folder and write activate. You can use cd .. to move around different environments.
<base> C:\Users\Sam\Environments\Venv\Scripts\activate
It will activate the virtual environment for you. How do you know that it has been activated? In the command prompt, you will see, in place of <base>, it will be <Venv>
<Venv> C:\Users\Sam\Environments\Venv\Scripts\
To deactivate the virtual environment, just write deactivate
<Venv> C:\Users\Sam\Environments\Venv\Scripts\deactivate
The above method only creates the virtual environment in Python 3. If you would like to create the virtual environment for Python 2, you need to install python 2 separately first and then follow similar steps.
Here is one of the many methods for it. To install python 2, go to your anaconda prompt and type,
conda create --name py2 python=2.7
Here py2 is the name of the environment (you can give any name of your choice). It will install the python 2.7 version. After installing it, find out where it has been installed. Usually, for anaconda, it gets installed in envs folder under Anaconda3. Noted down the path of the folder. As an example path: 'C:\Users\Sam\Anaconda3\envs\Py2\python.exe'
Now go to your folder where you want to create virtual environment and write in one line virtualenv -p "the path"
<base> C:\Users\Sam\Environments\Python2\ virtualenv -p C:\Users\Sam\Anaconda3\envs\Py2\python.exe Venv2
It will create a Python 2 virtual environment for you.
Now you have found out the way to install the virtual environments. One thing is still missing, especially for folks who are very interested in data science, machine learning, etc. and that is how to use the newly created virtual environment in Jupyter notebook? Well, there is a way. Go to your virtual environment, activate it and then write pip install iPykernel. After you have installed the package, you need to connect it with the Jupyter notebook. For this, you need to write the following
pip install ipykernel ipython kernel install --user --name=Venv
here Venv is the name of the virtual environment. That's it. Now when you create a new file in Jupyter, click on new and you will see Venv in the dropdown menu.
That's it for now. Hope it will help you. If you know someone who could get benefit from it, please share it with them and let me know your thoughts.
Battle-hardened Technologist | Hands-on programmer | Still Learning
4 年Don't rely on virtual env for deploying python. Venv are quite useful for quick developments/test. For deployment I will prefer containers.