Creating virtual environments is essential, because if you end up installing all the latest libraries required for your projects, you might end up breaking the code or application for another project that requires another version or an older version of the packages. Therefore, the best solution is to create a Virtual Environment that allows you to work on independent projects in isolation to other projects without having an conflict in library versions.
1. Run python -m venv ‘foldername’
2. cd box (for instance if you named your folder box)
3. For windows, run the following command in your CLI: .\Scripts\activate , and press Enter. For Linux/Mac, run the following command to activate the Virtual Environment: source venv/bin/activate
4. After activation, your terminal prompt will change to indicate the virtual environment is active, e.g., `(venv)`.
5. You will be in your virtual environment, now you can install packages inside your project that is running in a virtual environment. Packages won’t get installed globally this way.
6. In order to run .env files, you need to install dotenv by running the following command: pip install python-dotenv
Quick Recap:
# Step 1: Create a project folder
mkdir my_project
cd my_project
# Step 2: Create a virtual environment
python -m venv venv
# Step 3: Activate the virtual environment
# For Windows
venv\Scripts\activate
# For Linux/Mac
source venv/bin/activate
# Step 4: Install python-dotenv
pip install python-dotenv
# Step 5: Deactivate when done
deactivate
Optional Commands
1. pip install –upgrade pip To upgrade your pip
2. To verify the installation of dotenv: *pip show python-dotenv
If you get the following error, then you may need to change the execution policy:
.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\venv\Scripts\Activate + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
This error occurs because **PowerShell’s execution policy** is set to restrict script execution, which prevents the activation script from running. Here’s how you can fix it:
Solution: Change the PowerShell Execution Policy
1. Open PowerShell as Administrator. press `Win + S`, type `Windows PowerShell`, right-click, and choose **”Run as Administrator”** or simply open directly.
2. Allow Script Execution. Run the following command to allow running locally created scripts:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
3. `-Scope CurrentUser`: Ensures the change only affects the current user (no admin rights required for this scope).
4. `RemoteSigned`: Allows locally created scripts to run, but requires remotely downloaded scripts to be signed.
5. Confirm the Change. You’ll be prompted with a confirmation message. Type `Y` (Yes) and press Enter.
6. Retry Activating the Virtual Environment. Navigate back to your project folder and activate the virtual environment: .\YourProjectFolderName\Scripts\Activate
7. Verify the Execution Policy You can check the current execution policy by running: Get-ExecutionPolicy -List. Ensure that the currentUser is remoteSigned.
1. Its a bit different to run your virtual environment through bash, you need to type different commands for that.
2. In Windows, virtual environments created with `venv` store activation scripts in the `Scripts` directory, but Git Bash requires a different command than PowerShell.
1. If you are outside the VENV folder then:
source venv/Scripts/Activate
2. If you are inside your VENV directory:
source Scripts/Activate
OUTPUT Example:
$ source Scripts/Activate
(pOne) ->This will appear under your command after you run it. This is your VENV folder.
1. After running the ***Bash activation command***, check:
which python
which pip
OUTPUT:
/e/MISCELLENEOUS/MY PROJECT/LEarning/Blockchain/Node js/My Assignments/Practice/FASTAPI/pOne/Scripts/python
(pOne)
/e/MISCELLENEOUS/MY PROJECT/LEarning/Blockchain/Node js/My Assignments/Practice/FASTAPI/pOne/Scripts/pip
(pOne)
NOTE: If the Virtual Environment is active, both the paths should point to your VENV folder.
2. If your virtual environment is not activated, then the output will show you the PATH of GLOBAL installation of pip and python.
3. Another Method is to use the following command to check if your Virtual Environment is active:
echo $VIRTUAL_ENV
OUTPUT:
/e/MISCELLENEOUS/MY PROJECT/LEarning/Blockchain/Node js/My Assignments/Practice/FASTAPI/pOne
(pOne)
NOTE: This command will return the path of the folder that has the virutal environment ACTIVE, and also mention in the Folder name inside parenthesis.