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.0 How to create a Virtual Environment in Python

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

1.1 Common Errors

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.2 Running Your Virtual Environment on *bash*

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.3 How to verify that Bash Shell is using Virtual Environment?

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.
  
  

1.4 File Structure of Virtual Environment

 
General steps to setting up your Virtual Environment
1. Create a project directory.
2. CD to your project directory.
3. Run the `python -m venv intro` command to create a Virtual Environment called intro (can be named anything)
4. This will create a folder for your Virtual Environment with all the configuration files and libraries.
5. Activate your Virtual Environment
6. Create a python file `touch app.py`, write some code.
7. Run your python file `python app.py`
8. Your file will now run in the virtual environment.
 
Different ways of structuring your project folder
 
1. Use the Virtual Environment as your project directory: You can create a virtual environment and keep all the project files inside the folder of your Virtual Environment. You project will work this way.
 
2. Creating 2 folders (Virtual Environment + Project Directory) You can create a separate folder outside the virtual environment and then run the project, it will work this way as well. Disadvantage is that your Parent directory will no longer be the project directory since you’ll have the project folder created separately. You will then have 2 folders(Virtual Environment + Project Folder). In this case you can use the following commands when in your parent directory:
a. pip install -r Data_cleaning_and_visualization/requirements.txt
b. You run the installation command from the parent directory as well: `pip install streamlit pandas matplotlib seaborn` And the packages will get installed in the requirements.txt file.
c. To create the requirements.txt file: `pip freeze > requirements.txt` This will create the requirements.txt file inside the folder you are in, if you want it to be inside your project directory, you will have to CD to project directory.
 
3. **Create a virtual environment inside your project directory:** Last and the best practice is to create a ***Project Directory***, cd to the project folder, then create a virtual environment. Inside the project directory start creating your project. This approach works best as this will enable you to keep a project directory name, and a Virtual Environment folder inside the Project directory, moreover you can use ***.gitignore*** to ignore the Virtual Environment folder when pushing your code, as long as you’ll have a requirements.txt file inside your main project directory, that would allow anyone cloning your project, and then creating their own virtual environment to run the project/app file.
 
4. Changing name of the project directory or Virtual Environment: By changing the names, you will have to delete the  Lib, Scripts and pyvenv.cfg files and then CD out of the Virtual Environment folder, and depending on the type of strategy you pick for your file structure, then create a new Virtual Environment again.
 

2.0 Are there Virtual Environments for other programming languages?

 
Virtual environments are not exclusive to Python! Many other programming languages have their own ways of creating isolated environments to manage dependencies.
 

2.1 Different types of Virtual Environments for different programming languages

 
1. Python → `venv`, `virtualenv`, `conda`:
a. `venv` (built-in for Python 3.3+)
b.`virtualenv` (third-party, more features)
c. `conda` (for data science & ML, works with non-Python packages too. 
d. Purpose: Isolates Python dependencies for different projects.
 
2. JavaScript/Node.js → `nvm`, `npm`, `yarn`, `pnpm`:
a. `nvm` (Node Version Manager) → Manages multiple Node.js versions.
b. `npm install –prefix ./myenv` → Installs dependencies in a specific directory.
c. `yarn` & `pnpm` → Package managers that can create isolated dependencies per project.
d. Purpose: Different Node.js projects can use different package versions.
 
3. Ruby → `rbenv`, `rvm`, `bundler`
a. `rbenv` & `rvm` → Manage multiple Ruby versions.
b. `bundler` → Manages project dependencies in an isolated `vendor/bundle` folder.
c. Purpose: Ensures different Ruby projects have separate dependencies.
 
4. Java → `SDKMAN!`, `Maven`, `Gradle`
a. **`SDKMAN!` → Manages multiple versions of Java, Groovy, Kotlin, etc.
b. `Maven` & `Gradle` → Dependency managers for Java projects.
c. Purpose: Ensures each Java project has the correct dependencies.
 
5. Go → `go modules` (`go mod`)
a. `go mod init` & `go mod tidy` → Creates an isolated module for Go projects.
b. Purpose: Allows different Go projects to have independent dependencies.
 
6. Rust → `cargo`
a. `cargo` → Rust package manager, keeps dependencies per project inside `Cargo.toml`.
b. Purpose: Isolates dependencies for different Rust projects.
 
7. PHP → `composer`
a. `composer` → PHP dependency manager (like `pip` for PHP).
b. `composer install –working-dir=my_project` → Installs dependencies for a specific project.
c. Purpose: Prevents global dependency conflicts.
Scroll to Top