Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Python»How to Install and Use virtualenv with Python 3

    How to Install and Use virtualenv with Python 3

    RahulBy RahulAugust 22, 20193 Mins ReadUpdated:May 24, 2020

    Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories and environment. This doesn’t share libraries with other environments. It is very helpful for the application required separate environments on the same server. The Virtualenv is the easiest and recommended way to configure a custom Python environment. This tutorial will help you to how to create a virtual environment for your Python application and use this.

    Prerequisites

    You must have the following packages installed on your system.

    • Python 3
    • PIP

    Install Virtualenv with Python 3

    You must have Python 3 and PIP installed on your system. We use virtualenv package to create virtual environment for the Python applications. So first install virtualenv Python module using command:

    pip3 install virtualenv
    
    Collecting virtualenv
      Downloading https://files.pythonhosted.org/packages/f7/69/9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB)
        100% |████████████████████████████████| 3.3MB 448kB/s
    Installing collected packages: virtualenv
    Successfully installed virtualenv-16.7.4
    

    Create Virtual Environment

    Search for the Python3 binary location on your system. Use the following command to find the Python 3 binary file location on your system.

    which python3
    
    /usr/bin/python3
    

    As per the above command, Python 3 binary is available at /usr/bin/python3.

    Next, switch to your Python application directory

    cd /var/webapps/
    

    Then, create a separate environment for your Application. You can change the environment directory name (isoEnv used in below example) as per your choice. You can also define the environment directory on a different location.

    virtualenv -p /usr/bin/python3 isoEnv
    
    
    Running virtualenv with interpreter /usr/bin/python3
    Already using interpreter /usr/bin/python3
    Using base prefix '/usr'
    New python executable in /var/webapps/isoEnv/bin/python3
    Also creating executable in /var/webapps/isoEnv/bin/python
    Installing setuptools, pip, wheel...
    done.
    

    The above command will creates a separate environment for the Python binary file. You can access it by typing python only.

    Activate Virtual Environment

    You need to activate virtual environment before using it. This will insure to install all modules and configurations to virtual environment only. To activate the virtual environment, execute:

    source isoEnv/bin/activate
    

    The name of the current virtual environment appears to the left of the prompt. Your prompt will be look like (isoEnv) [email protected]$

    To verify the correct Python version, run the following:

    (isoEnv) [email protected]$ python -V
    
    Python 3.6.8
    

    Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation.

    Use pip3 to install a module:

    (isoEnv) [email protected]$ pip3 install <module>
    

    You can use ‘nose’ if you’re going to work with openstack. For example:

    (isoEnv) [email protected]$ pip3 install nose
    
    Collecting nose
      Downloading https://files.pythonhosted.org/packages/15/d8/dd071918c040f50fa1cf80da16423af51ff8ce4a0f2399b7bf8de45ac3d9/nose-1.3.7-py3-none-any.whl (154kB)
         |████████████████████████████████| 163kB 18.6MB/s
    Installing collected packages: nose
    Successfully installed nose-1.3.7
    

    Deactivate virtualenv Environment

    After finishing your work inside the virtual environment, simply exit from this by typing deactivate command. You will get the users to default shell.

    (isoEnv) [email protected]$ deactivate
    

    Delete virtualenv Environment

    If your application no more required the virtual environment, You can delete this. To delete the environment simply delete the environment directory.

    rm -rf isoEnv
    

    Conclusion

    In this tutorial, you have learned about configuration of the Python virtual environment on Linux system.

    Python Python3 virtualenv
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Pass Command Line Arguments to Python Script
    Next Article How To Install Django on Fedora 35/34/33

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    How to Install Python 3.10 on CentOS/RHEL 8 & Fedora 35/34

    Updated:February 12, 20223 Mins Read

    How To Install Python 3.10 on Ubuntu, Debian & Linux Mint

    Updated:April 22, 20222 Mins Read

    How to Switch Python Version in Ubuntu & Debian

    Updated:April 22, 20223 Mins Read

    Working with Python Functions

    6 Mins Read

    2 Comments

    1. petjan on May 25, 2020 5:56 am

      why not python3 -m venv /path/to/venv ?

      Reply
    2. Mohan Phatak on October 24, 2019 11:29 am

      Thank you for python 3.8 installation post. I am learning python . I am senior citizen. I hope I can get help from you from time to time.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.