SoFunction
Updated on 2025-03-10

How to create and use Python virtual environment (using the built-in venv module)

1. Install the virtual environment tool

Starting from Python 3.3, Python comes with itvenvModules, no additional installation is required. You can use it directly to create a virtual environment.

2. Create a virtual environment

2.1 Create a virtual environment using venv

Use the following command to create a virtual environment. I used it herevenvTo create a virtual environment and name itvenv, you can also choose any other name.

python -m venv myvenv

  • python -m venv myvenv: This command will create a virtual environment and folder named myvenv in the current directory.
  • If you have multiple Python versions, you may need to specify a Python version, such aspython3.8orpython3to ensure the correct version is used.

2.2 View virtual environment files

After the virtual environment is created, a myvenv folder will be generated in the current directory. It contains the files and directory structures required for the virtual environment:

  • bin: Executable files containing virtual environments (such aspython)。
  • lib: A library file containing the virtual environment.
  • include: Contains the header file used to compile the C extension module.
  • Scripts(Windows): IncludesWait for scripts.

3. Activate the virtual environment (under Windows)

  • .\myvenv\Scripts\activate

    After executing, you will see the name of the virtual environment appearing in front of the command line.(myvenv), indicating that the virtual environment has been activated.

  • After activation, you will see the command line is preceded by(myvenv), which means that you have entered the virtual environment.

  • Or enter the myvenv directory and run itScripts\activate

4. Use a virtual environment

When the virtual environment is activated, you can install and manage Python packages in the virtual environment. All passedpipThe installed package will only affect the current virtual environment, but will not affect the global Python installation.

4.1 Installing dependency packages

You can use it in a virtual environmentpipTo install the dependency package you need:

pip install <package_name> 

4.2 View installed packages

You can usepip listView all packages installed in the virtual environment:

pip list 

4.3 Uninstall the package

If you no longer need a package, you can usepip uninstallUninstall it:

pip uninstall <package_name>

5. Generate

Files are a common way to record project dependency packages, and are usually used to share and reproduce environments.

5.1 Create

You can usepip freezeThe command generates a list of dependency packages for the current virtual environment and saves it toIn the file:

pip freeze >

This command will record all installed packages and versions in the virtual environment toin the file.

5.2 Dependencies in Installation

When others get your project code, they can useAll dependencies required for installing the project:

pip install -r  

6. Exit the virtual environment

When you finish your work, you can exit the virtual environment by:

deactivate

After exiting, your command line will return to the system's default Python environment.

7. Delete the virtual environment

If you no longer need a virtual environment, you can delete it. Just delete the folder containing the virtual environment (usuallyvenvfolder).

8. The benefits of using virtual environments

  • Quarantine dependency: Each project has its own dependency package to avoid version conflicts.
  • A clean working environment: Library versions between different projects will not affect each other.
  • Easy to deploy:passFiles, you can easily deploy projects for other developers or production environments.

IDLE Shell Use Virtual Environment

Python IDLE Shell can use virtual environments, but it is slightly different to set up because IDLE starts the system Python environment by default. To use a virtual environment in IDLE, you need to manually specify the Python interpreter in the virtual environment.

myenv\Scripts\ -m

This way, IDLE will start and use the Python interpreter in the virtual environment. You can execute code in IDLE and make sure it uses dependencies installed in the virtual environment, rather than libraries in the global Python environment.

Note: No other packages can be installed until IDLE is closed. Because when IDLE is displayed, the command is still executing.

Check whether IDLE uses virtual environment

In IDLE, you can check whether the current Python environment is a virtual environment by running the following command:

import sys 
print()

Summarize

This is the end of this article about the creation and use of Python virtual environments. For more related content on creating and using Python virtual environments, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!