Technology Sharing

Python's virtualenv virtual environment common problems and commands

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

commonvenvOrder

In Python,venvIt is a module for creating and managing virtual environments. Virtual environments can help you isolate different Python packages and dependencies between projects. The following are some commonly usedvenvOrder:

Create a virtual environment:

#配置了
python3 -m venv myenv
python -m venv myenv

This will create a new directory calledmyenvVirtual environment.

Activate the virtual environment:

  • On Windows:

    ./venv/Scripts/activate
    
  • On macOS and Linux:

    source myenv/bin/activate
    

    After activating the virtual environment, Python commands you run in the terminal will use the Python interpreter and packages in the virtual environment.

Verify that activation is successful

  • implement pip list to see whether the dependency is global or in the current virtual environment.
  • Is there a command line before (venv) Logo.

Exit the virtual environment:

Execute in a virtual environment deactivte , which will exit the currently activated virtual environment.

View installed packages:

pip list

This will list the installed Python packages in the current virtual environment.

Installation package:

pip install package_name
pip install -i https://mirrors.aliyun.com/pypi/simple package_name

This will install the specified Python package in the current virtual environment.

Export dependencies:

pip freeze