Create a virtual environment for Python in the Win10 environment

What is a PYTHON virtual environment

A Python virtual environment is an isolated environment for isolating Python projects. Used to isolate dependencies of different projects. It allows you to manage multiple projects on the same computer, each with its own independent Python runtime and dependencies. By creating a virtual environment, you can manage different dependent libraries and versions of multiple projects on the same computer at the same time, avoiding conflicts between them.

Using a virtual environment we can do:

  1. Dependency isolation: Each virtual environment has its own independent Python interpreter and installed libraries, so dependency conflicts between different projects can be avoided.

  2. Environment Consistency: Virtual environments can ensure that projects have the same dependencies and configuration across environments, providing better portability and consistency.

  3. Simplified dependency management: By using virtual environments in your projects, you can use project-specific versions of dependencies, and install and update dependencies in virtual environments without affecting the global Python environment.

Common Python virtual environment management tools

Common Python virtual environment management tools mainly include the following:

  • venv: Python's own virtual environment module, suitable for Python3.
  • virtualenv: A third-party tool that can create virtual environments in Python2 and Python3.
  • conda: A powerful tool for creating and managing virtual environments, especially useful for scientific computing and data analysis projects.
  • pyenv: A version manager that can switch global Python versions and create local virtual environments.
  • pipenv: A tool that combines virtual environments and package management tools for developing Python projects.

Advantages, disadvantages and applicable scenarios of common management tools

  1. venv:

    • Advantages: Python comes with itself, no additional installation is required. Lightweight and fast to create.
    • Cons: Only works with Python3.
    • Applicable scenarios: Simple virtual environment management suitable for Python3 projects, especially projects that already have required modules in the standard library.
  2. virtualenv:

    • Advantages: Support Python2 and Python3. Powerful, you can customize the directory structure of the virtual environment.
    • Cons: Additional installation required.
    • Applicable scenarios: suitable for creating virtual environments of Python2 and Python3, and projects that require custom virtual environment structures.
  3. conda:

    • Advantages: Powerful, can manage Python environment and non-Python environment. Has a large number of precompiled scientific computing and data analysis packages. Cross-platform support is good.
    • Disadvantages: The file size is large and the installation process is slow. When using the conda command, sometimes there is an incompatibility with pip.
    • Applicable scenarios: Suitable for scientific computing and data analysis projects, especially those that need to manage non-Python environments.
  4. pyenv:

    • Advantages: It supports switching the global Python version, which is convenient for managing multiple Python versions. A local virtual environment can be created.
    • Cons: Cannot create multiple independent Python environments.
    • Applicable scenarios: Suitable for projects that want to manage multiple Python versions and create local virtual environments.
  5. pipenv:

    • Advantages: It integrates virtual environment and package management tools, and the operation is simple and convenient. Automatically manage package and dependency conflicts required by your project.
    • Cons: Sometimes slow, especially when installing packages. Not very suitable for large and complex projects.
    • Applicable scenarios: Suitable for small or medium-sized Python projects, especially those that need to automatically resolve package dependencies.

Create a virtual environment

Here virtualenv is used to create a virtual environment. First, create a new OAK folder in the root directory of the D drive.

  • Open a terminal or command line interface.

  • Enter the target folder where we want to create a virtual environment, the OAK folder.

  • Enter virtualenv OAKenv commands to create a virtual environment; tools are used here virtualenvto create a virtual environment. OAKenvAfter running this line of command, a folder named python will be created in the current directory , which will contain an independent Python environment.

  • Activate the virtual environment: Activate the virtual environment by entering the following command: OAKenv\Scripts\activate; After activating the virtual environment, we can use pipthe command to install the dependencies required by the project and ensure that these dependencies are only visible to this virtual environment and will not affect other projects. If you want to exit the virtual environment, you can run the command directly on the command line deactivate.

    Note here: Every time we want to use a virtual environment, we need to activate the virtual environment first to take effect. Activating a virtual environment will set that environment's Python interpreter and installed libraries as the default Python runtime for our current terminal session.

    Once a virtual environment is activated, any Python command or script we run in the terminal will use that virtual environment's Python interpreter and installed libraries. When we complete the project development or no longer need the virtual environment, we can use the command provided by the virtual environment management tool to exit the virtual environment to return to the global Python environment.

    It should be noted that the activation of the virtual environment is only valid for the current terminal session. If we open a new terminal window or start a new terminal session, we need to reactivate the virtual environment to use it in the new terminal.

  • Install dependencies: Once we activate a virtual environment, it will provide an independent Python runtime environment, which means we need to reinstall the libraries and dependencies we need in this environment. For example, if I need depthaia library here, we need to execute the following instructions to install the dependent library

    pip install depthai-sdk
    

    After the dependency installation is complete, we can use the following command to view the list of installed packages

    pip list
    

    pip listThe command is used to list all packages and their version information installed in the current Python environment. It can help us view installed packages, and their version numbers, which is very useful when managing and maintaining a Python environment.

    Executing pip listthe command will display a table with installed package names and corresponding version numbers. Examples are as follows:

    Package             Version
    ------------------- -------
    numpy               1.21.0
    pandas              1.3.0
    scikit-learn        0.24.2
    tensorflow          2.5.0
    

    From this list, we can know which packages are installed in the current Python environment and their version numbers. This is useful for checking if a package needs updating, or to view installed dependencies.

    Also, we can change the behavior of the command by adding some options pip list. For example, --outdatedan option can list outdated versions of all installed packages, which is useful for finding packages that need to be updated. Example:

    pip list --outdated
    

FAQ

  1. Does the virtual environment need to be created every time?
    Virtual environments don't need to be created every time, especially for projects that already exist.

If we have already created a virtual environment in the project, we can directly reuse the virtual environment without recreating it when working in other environments or on other machines.

We can copy the project's virtual environment directory (usually a folder containing the Python interpreter and dependent packages) to another location, and then activate the existing virtual environment in the new location.

The advantage of this is that we can keep the dependency environment of the project consistent and facilitate sharing and collaboration with team members.

Note that if we have different dependencies between different projects, it is best to create separate virtual environments for each project to ensure isolation between projects and clarity of dependency management.

  1. Is activating the virtual environment activated every time the project is run?
    Every time you run a project, you usually need to activate the virtual environment.

The activation process of the virtual environment will set the environment's Python interpreter and dependencies as the default interpreter and dependencies in the current running environment. This ensures that the project is running with the correct Python version and dependencies.

  1. Do I have to install dependencies every time I activate a virtual environment?

    Every time you activate the virtual environment, you don't need to reinstall the dependencies that have already been installed, unless you add new dependencies.

    When we first create a virtual environment, we need to install the dependencies required by the project. This can be done by running the pip install command. For example, we can run the following command to install a project's dependencies:

    pip install -r requirements.txt
    

    requirements.txt is a file that contains all dependencies of the project and their version numbers.

    Once the dependencies are installed, we only need to install the new dependencies when activating the virtual environment. When we add a new dependency package, we can install the new package by running the pip install command, or add the new package to the requirements.txt file and run the pip install -r requirements.txt command to install it.

    After activating the virtual environment, we can run your project with the installed dependencies. Therefore, every time you activate the virtual environment, you don't need to reinstall the already installed dependencies, unless you add new dependencies.

Guess you like

Origin blog.csdn.net/w137160164/article/details/131426743