venvpy

📦Creating a Virtual Environment in Python

image

👉HIGHLY RECOMMEND VS Code FOR THE VIRTUAL ENVIRONMENT SETUP

A virtual environment is a self-contained Python environment that allows you to isolate and manage your Python dependencies for different projects. This is important when:

🔗Why Use Virtual Environments?

Isolation of Dependencies 🔒

In Python, different projects often require different sets of packages and dependencies. Installing all packages globally can lead to conflicts and compatibility issues. Virtual environments provide a solution by allowing you to create isolated environments for each project.

With virtual environments, you can:

Reproducibility 🚀

Virtual environments help ensure that your project remains reproducible. When you create a virtual environment and list the project’s dependencies in a requirements.txt file, you can recreate the exact same environment on another machine or for a colleague.

This is especially useful when:

⚙️Creating Virtual Environments

Here’s how to create a virtual environment using the python -m venv command:

🚪Deactivating the Virtual Environment

To deactivate the virtual environment and return to the global Python environment, run:

deactivate

This will deactivate the virtual environment and restore your Python environment to its default state.

Issues that might come

image

If this issue comes then this means that you’re encountering an issue with PowerShell’s execution policy, which is preventing the activation script from running. PowerShell’s execution policy is a security feature that controls the conditions under which PowerShell loads configuration files and runs scripts.

To resolve this issue, you can temporarily change the execution policy for the current PowerShell session to allow script execution. Here’s how you can do it:

Set-ExecutionPolicy RemoteSigned -Scope Process
.\myenv\Scripts\Activate

This should allow you to activate the virtual environment without encountering the security error. Once you’ve finished working in the virtual environment, you can close the PowerShell session, and the temporary execution policy change will revert to the previous state.

To permanently set the execution policy for PowerShell, you’ll need to run the Set-ExecutionPolicy command with appropriate parameters. Here’s how you can do it:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This command sets the execution policy to RemoteSigned for the current user only. It allows scripts that are downloaded from the internet to run if they are signed by a trusted publisher.

If you want to set the execution policy for all users on the computer, you can use the AllSigned policy, which requires all scripts to be signed by a trusted publisher. However, this may be more restrictive:

Set-ExecutionPolicy RemoteSigned -Scope LocalMachine

Be cautious when setting the execution policy, especially to a less restrictive one like RemoteSigned, as it can pose security risks if you run untrusted scripts. Always ensure that you only execute scripts from trusted sources.


Now you know how to create a virtual environment in Python on different operating systems and why it’s important. Virtual environments are essential for managing dependencies, ensuring project isolation, and maintaining reproducibility in your Python projects. 😊

Made with 💖 by Saad