• How to Set Up Virtual Environments in Python on Windows and Linux?

    The Python Virtual Environments system denotes a tool that is used to keep different packages in different spaces to avoid conflicting issues. Such environments remain isolated from the other Python projects or environments and ensure that the environment comes with its dependencies to stay independent.

    Installing Python in different operating systems like Windows, and Linux needs to follow different installation methods. Check out the installation steps here.

    Setting up Python Virtual Environment in Windows 

    Install Python First 

    If you are using an ENGINEERING Windows Desktop, you can install Python using the 2.7 through the software center.
     

    Go for Adding Python to the Path 

    These steps involve:

    • Navigate: Go to the Control Panel then go to the User Accounts and Change my Environment Variables
    • Inside the top window  marked ‘User Variables’ select ‘Path’ and edit it
    • Go to and select ‘Browse’ and navigate to C:\python27 and click on ‘ok’
    • Select the ‘Move Down’ option and repeat step 3 for C:\python27\Scripts

    The next step would be opening a new CMD prompt  by using Windows Key + R, cmd.exe

    In this step, use the command pip install virtualenv and install virtualenv. If you are not the administrator, avoid updating even if you get the prompt.
     

    At the last step, make sure virtualenv gets installed through the pip –version command.

    How to Test the Functionality?

    • Go for creating a test environment mkvirtualenv example and this one will create a folder named Envs in your specific user directory. 
    • At this step, change the Python directories to project folders in your command prompt and run the code setprojectdir to set it as the development folder. 
    • Use deactivate to leave the environment. 
    • With the help of workon example you have the option to enter an environment and from that place invoke the pip install flask package installations. 
    • When you want to delete the environment totally, leave the environment first and use the rmvirtualenv example for deleting.

    Setting up Python Virtual Environment in Linux 

    Start with creating virtual environments on Python 2.7
    • Go for creating a virtual environment in the recent directory for a project using the command 7virtualenv my_project.
    • Name the project as per your wish
    • If you want a virtual environment with a specific Python version, use the command: virtualenv -p /usr/bin/python2.7 my_project
    • You have the option to replace “python2.7″ with your preferred version.
    Creating Virtual Environments on Python 3.5+
    • For this Python version, use the command python3 -m venv my_project in your current directory.
    • Name the environment as per your wish.

    How to Use Virtual Environment?

    Bash Shell 

    Start working on it by activating the command source my_project/bin/activate

    csh /tcsh:

    In csh /tcsh, use the command source my_project/bin/activate.csh to start the virtual environment.

    You can use pip as usual for installing packages 

    If you are running ‘python‘ it will be the version specified in Step 1

    Running ‘pip‘ means running the current version

    Be sure all modules are installed inside that environment

    After you are done with working in the environment, use the deactivate command to exist.

    Go after these steps for a successful virtual environment installation and make sure to set up the environment correctly.

© 2024 Euphoriagenx. All Rights Reserved