Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Python»How to Create Python Virtual Environment on Ubuntu & Debian

    How to Create Python Virtual Environment on Ubuntu & Debian

    By RahulJanuary 3, 20234 Mins Read

    Python’s venv module is used for creating virtual environments. It helps us to create and manage isolated environments for Python applications with different versions of Python. These are lightweight environment that has their own independent set of Python packages in their site directories.

    Advertisement

    The Python virtual environment is used to isolate your project’s dependencies and avoid conflicts between projects. If you are a Python developer, DevOps, or a Linux System administrator, you would have worked on Python virtual environment at some point in time.

    In this blog post, we will walk you through the steps necessary to create a virtual environment for Python development on Ubuntu and Debian Linux systems.

    Prerequsities

    This article assumes, that you already have Python installed on your Ubuntu, Debian, or Linux Mint systems.

    Step 1 – Install venv

    First of all, you need to install the Python module for the virtual environment on your system. Python3 users can directly install the package for the env. The Python 2.7 users need to install virtualenv Python module. This will also install other required modules on your system.

    • For Python3:
      sudo apt install python3-venv 
      
    • For Python 2.7:
      sudo pip2 install virtualenv 
      

    Step 2 – Create Python Virtual Environment

    Once the installation is finished. Let’s create an isolated Python environment for your application.

    1. Locate the python binary files location on your system. I have just installed Python 3.10 and Python 2.7 on our Debian system. In my case both binary files are located at /usr/bin/python3.10 and /usr/bin/python2.7.
      which python2.7 
      #Output: /usr/bin/python2.7 
      
      which python3.10 
      #Output: /usr/bin/python3.10 
      

      You can choose any other Python version as per the application requirements.

    2. Now, Create a separate environment for your Python Application. We are using venv as the environment directory name, but you can use any other name of your choice. You can also define the environment directory in a different location.

      First, navigate your Python project directory.

      cd myPythonApp 
      
    3. Then create Python isolated environment based on the Python version requirements.
      • For Python 3:
        /usr/bin/python3.10 -m venv venv 
        
      • For Python 2.7:
        virtualenv -p /usr/bin/python2.7 venv 
        

    The above commands create a directory named venv in the current directory with a local copy of files. While working on this website, you should activate the local environment in order to make sure you’re working with the right versions of your tools and packages.

    Step 3 – Activate Python Virtual Environment

    To work with a Python virtual environment, you need to activate the environment. After that, you can install a required module for your Python project as well as run your Python application in an isolated environment.

    Use the following command to activate the Python environment:

    source venv/bin/activate 
    

    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. To install the most commonly used ‘requests’ module, type:

    pip3 install requests 
    
    Output
    Collecting requests Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB) |████████████████████████████████| 62 kB 1.8 MB/s Collecting idna<4,>=2.5 Downloading idna-3.2-py3-none-any.whl (59 kB) |████████████████████████████████| 59 kB 12.0 MB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.7-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 35.3 MB/s Collecting charset-normalizer~=2.0.0 Downloading charset_normalizer-2.0.6-py3-none-any.whl (37 kB) Collecting certifi>=2017.4.17 Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB) |████████████████████████████████| 145 kB 36.4 MB/s Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests Successfully installed certifi-2021.5.30 charset-normalizer-2.0.6 idna-3.2 requests-2.26.0 urllib3-1.26.7

    All the installed modules files are placed at venv/lib/python3.10/site-packages directory.

    Step 4 – Deactivate Python Virtual Environment

    After finishing your work inside the virtual environment, just type the “deactivate” command to exit from the isolated environment prompt.

    deactivate 
    

    You will get the default system prompt.

    Step 5 – Deleting the Python Virtual Environment

    To Delete the Python virtual environment from your application. Simply delete the venv directory from your application folder.

    rm -rf venv 
    

    Conclusion

    In this tutorial, you have learned to create Python virtual environment on Ubuntu, Debian, and other Debian derivative Linux systems. The Python virtual environment helps us to deploy multiple Python applications on a single server without making conflicts for modules between each other.

    env Python virtualenv
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Install Python 3.11 on Amazon Linux 2

    Installing Python 3.11 on Debian Linux

    How To Install Python 3.11 on Debian 11/10

    Installing Python 3.11 on Ubuntu, Debian and LinuxMint

    How To Install Python 3.11 on Ubuntu, Debian and LinuxMint

    View 2 Comments

    2 Comments

    1. Mohammad javad on February 9, 2021 11:49 am

      thank you very much, sir

      Reply
    2. Pedro Riva on June 20, 2020 7:25 am

      The official recommendation is to use venv instead of virtualenv. What do you think about?

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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