Activating of Virtual Environment in Python

Are you tired of writing env_name_of_your_choice\scripts\activate {e.g. env\scripts\activate} or env_name_of_your_choice\source\bin each time you want to activate your virtual machine? If yes, then I need you to install the latest Vscode and Python in your machine, if you don't have one.

Python Virtual Environment (PVE) can simply be defined as an isolated environment of your project in your machine that cannot affect any existing environment in your machine. So, each time you create a Python project, you need to create a new virtual environment in that specific project and with this PVE of your name choice (for example env) you can install any Python packages you want which are isolated from other projects in your operating system. Thus, the Python packages you installed in each environment can be of the same version or different versions which are isolated in their respective Python project folders.

For Windows Users:

On a window machine using your vscode terminal, select 'the terminal at the very top icon of your vscode or simply use your shorthand, ctrl + shift + `, once your terminal is open with your project profile. Your vscode terminal is the same as your window terminal, it's just an integrated part of your terminal in your window to avoid you continuously flipping back and forth while working on your project.

Steps to create and keep your virtual-environment running on your machine:

  1. pip install virtualenv and press enter -> which will install PVE in your current project directory if you don't have on already.

  2. virtualenv "env_name_of_your_choice and press enter" (e.g. virtualenv env) which automatically creates your env folder for you in your project directory.

  3. we want to continuously use our Python execution interpreter which is isolated in our project directory instead of using the global Python interpreter that comes with Python when you installed it in your system. To see this, happen, open your env folder we just created, then open the Scripts subfolder of your env, there you will see your 'python.exe' which we have to now activate to be our interpreter henceforth throughout the lifecycle of our project development. Let's jump back in to accomplish this, shall we?

  4. On the top left side of your vscode, click on the view icon and select the 'command palette' that appears on the dropdown menu. Then search for 'python: select interpreter', so select 'Enter path to a Python interpreter...' and we will give it our path from the root of our project directory, '.\env\Scripts\python.exe' and then press enter.

  5. env\Scripts\activate.bat and press enter. You must see the env first on the left side of your project directory. Once you want to activate it next time, use this same command.

  6. then using the alias command on your Linux/bash command, type alias env="scripts\env\activate" on your terminal, any time you want to activate you just type env, thus no more three-word-path activation.

Thank you for reading, and don't forget the share !

#Python #django #environment #os #flask #FastAPi