How to create virtual environment from terminal (command line)

Farzanajuthi
3 min readAug 19, 2023

--

There are many reasons for creating virtual environment. I have mentioned two of them.

  • In our system, there are different type of projects and they need different versions of packages or libraries. If we install all dependencies in our system directly, then there will be conflict in different versions. To avoid this we need a virtual environment where we can install specific versioned library for specific projects.
  • Other benefit of creating virtual environment is to protect your system from polluting with different libraries. If you use virtual environment, you can get a clean and organized environment.
Photo by arzhost.com

To create a successful environment, follow the steps

  1. To open command line based on your operating system choose one command:
For windows:
Short cut key: Win + R
Then write "cmd" and press "Enter" button

For Linux:
Short cut key: Ctrl + Alt + T


For Mac:
Short cut key: Command + Space
Then write "Terminal" and press "Enter" button


Here "+" sign meaning - press all these keys at a time.

2. If you want to go to specific folder, then run following command

Command:     cd <path of your destiantion>
Example: cd Documents/

3. Then make a folder.

Command:     mkdir <folder_name>
Example: mkdir video_processor
Create folder

4. Then go into this folder by running following command:

Command:     cd <folder_name>
Example: cd video_processor

5. If you are using any environment previously deactivate it.

deactivate

If you are using Conda previously deactivate it using following command.

conda deactivate

6. If you are using python version 3.3 or later use following command to create a virtual environment. Here I named it as myenv.

python -m venv <your_env_name>
Example: python -m venv myenv

If you are using python version before 3.3 then use following command one after another.

1. pip install virtualenv
2. virtualenv myenv
After creating virtual environment myenv

7. After creating your virtual environment, you have to activate this. To activate it, you have to run following commands one after another.

Based on your operating system, run the command. 
Here myenv is the name of my virtual envirnment.

For Windows
command:
<your_env_name>\Scripts\activate
Example: myenv\Scripts\activate

For Mac/Linux
command:
source <your_env_name>/bin/activate
Example: source myenv/bin/activate

Now you successfully create your virtual environment. What do you want next? Do you want to make a file to store your library name and run all these dependencies from there? Follow the steps outlined in rest of this post.

To create a file named “requirement.txt”, you have to run one of the commands based on your operating system and it will create an empty file:

For Windows:
Command: type nul > filename.ext
Example: type nul > requirement.txt
For Mac/linux:
Command: touch filename.ext
Example: touch requirement.txt

Here,
filename = you will give your file name
ext = extension of your file like py, txt, pdf etc
Create a file from terminal

Now you have to give library name into this newly created file. You can do this in following way. Here I give 2 library name numpy and matplotlib.

Command:     echo "<library_name>" >> <file_name>

Example: echo "numpy" >> requirements.txt
echo "matplotlib" >> requirements.txt
Libraries in requirement.txt files

Then run following command to install these libraries into your virtual environment:

Command:     pip install -r <your_file_name>

Example: pip install -r requirements.txt
After installation, library will be shown in lib folder in virtual environment

--

--

Farzanajuthi

I am an AWS community builder. I have passed AWS certified solution architect (CO3) exam). I love serverless technology and also share knowledge with others.