Creating virtual environment with requirements file

Sworu Raj Poudel
3 min readJun 19, 2021

--

Hello learner, today I will make you familiar with creating virtual environment(env) with requirements file before that lets understand what is virtual environment and why it is needed.

Virtual env is separate place or directory in your computer where necessary libraries with specific version can be install for particular project. Same time you might be working on different project with different libraries and version so creating virtual env can solve the conflict for your project. It means you can install different version of same library in different environment.

There are different way to create virtual environment but here I am using command line.

Simply press window+R to open run and type cmd then press Enter.

you will see something like this. Now change the directory where you want to create your virtual environment. Here I am going to create into desktop with folder name called my_project1

In command line type cd desktop and press Enter. After that type mkdir my_project1 and press Enter. This will change your directory to desktop and create new_folder called my_project1 (you can write any name you want).

Now type python -m venv my_project1\venv and press Enter. Here venv after -m is module for creating virtual environment and venv at last is folder inside my_project1 where virtual environment file are found.

virtual environment is already created. Now lets activate our just created environment for that type my_project1\venv\Scripts\activate and press Enter. when you do this you will see venv in small bracket on command line which show your environment is activated. To deactivate environment simply type deactivate and press Enter.

You can now install any libraries by simply typing pip install library_name when you are activate in virtual env.

Like to install pandas and requests libraries type pip install pandas requests(without comma)and press Enter.

Lets see which libraries are already available there and I will install requirement files inside this environment for that type pip list and press Enter. This will show you available libraries with version.

you can see two package/libraries that are install which are env dependent. Now we want to install libraries according to requirements. For this we need requirements.txt file.

now to install all this requirements we are going to type in command line in this way.

pip install -r requirements.txt and press Enter. It will take some time and internet connection is necessary.

requirement.txt file path should be provided correctly otherwise it will through error. at last to see install libraries we can type pip list where you get all libraries which has be install through requirements.txt file. Finally you can type deactivate and press Enter to deactivate you virtual env.

CONGRATULATIONS you just created your virtual environment with requirements.txt file. Always try to use virtual env for any project to make management of file and libraries easy.

--

--

No responses yet