SoFunction
Updated on 2025-03-04

One article teaches you how to create a venv Python virtual environment

Creating a Python virtual environment is a good practice that can help us manage project dependencies and avoid conflicts between different projects. The following is usedvenvDetailed steps to create a Python virtual environment for modules:

Create a virtual environment using the venv module

venvIt is a module that comes with Python and is available since Python 3.3. The following are the specific steps:

1. Create a virtual environment

Suppose you want to create a name calledmyenvFor virtual environment, you can use the following command:

python -m venv myenv
  • python: Make sure this is the Python interpreter you want to use.
  • -m venv: CalledvenvModule to create a virtual environment.
  • myenv: The name of the virtual environment, you can change it as needed.

2. Activate the virtual environment

After activating the virtual environment, your command prompt will display the name of the virtual environment, indicating that you have entered the virtual environment.

.\myenv\Scripts\activate

After activation, the command prompt will become:

(myenv) C:\path\to\your\project>

3. Install dependencies

In the activated virtual environment, you can usepipInstall the required dependencies. For example, installrequestsLibrary:

pip install requests

4. Verify the installation

You can verify installed packages to make sure they are installed in a virtual environment:

pip list

5. Exit the virtual environment

When you finish your work and want to exit the virtual environment, you can use the following command:

deactivate

After exiting, the command prompt will return to its original style.

Example

Here is a complete example showing how to create and use a virtual environment:

# Create a virtual environmentpython -m venv myenv

# Activate the virtual environment.\myenv\Scripts\activate

# Install dependenciespip install requests

# Verify installationpip list

# Exit the virtual environmentdeactivate

Create a virtual environment using virtualenv (optional)

You can follow the following steps:

1. Install virtualenv

pip install virtualenv

2. Create a virtual environment

virtualenv myenv

3. Activate the virtual environment

.\myenv\Scripts\activate

4. Install dependencies

pip install requests

5. Verify the installation

pip list

6. Exit the virtual environment

deactivate

Summarize

With the above steps, you can easily create and manage Python virtual environments, ensuring that your project's dependencies are independent and do not conflict with other projects.

This is the article about this article teaching you how to create a Python virtual environment venv. This is all about this. For more related content on Python virtual environment venv creation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!