DEV Community

farzana-juthi
farzana-juthi

Posted on • Originally published at Medium

How to create virtual environment from terminal (command line)

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 โ€”

  • 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.
Enter fullscreen mode Exit fullscreen mode
  • If you want to go to specific folder, then run following command
    Command:     cd <path of your destiantion>
    Example:     cd Documents/
Enter fullscreen mode Exit fullscreen mode
  • Then make a folder.
    Command:     mkdir <folder_name>
    Example:     mkdir video_processor
Enter fullscreen mode Exit fullscreen mode

Create folder

  • Then go into this folder by running following command:
    Command:     cd <folder_name>
    Example:     cd video_processor
Enter fullscreen mode Exit fullscreen mode
  • If you are using any environment previously deactivate it.
    deactivate
Enter fullscreen mode Exit fullscreen mode

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

    conda deactivate
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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

    1. pip install virtualenv
    2. virtualenv myenv
Enter fullscreen mode Exit fullscreen mode

After creating virtual environment myenv

  • 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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

After installation, library will be shown in lib folder in virtual environment

Top comments (0)