Using a python virtual environment, why should the virtual environment be activated

Using a python virtual environment, why should the virtual environment be activated

pre-foundation

The concept of python modules and packages

Python module (Module)

A Python module (Module) is a Python file ending in .py that contains Python object definitions and Python statements.
Modules can define functions, classes, and variables, and modules can also contain executable code.
There are three types of modules:
(1) Built-in standard modules (also known as standard libraries), execute help('modules') to view a list of all python built-in modules. Such as sys, os, math, etc.;
(2) Third-party open source modules can be installed through "pip install module name". Such as numpy, Pandas, Matplotlib, etc.;
(3) Custom modules, that is, create a .py file, which can be called a module and can be imported in another program. That is, edit the .py file.

In Python, each Python file can be regarded as a module, and the name of the module is the name of the file . That is to say, the custom module name must conform to the identifier naming rules.

A module is only imported once, no matter how many times you import it. This prevents imported modules from being executed over and over again.

Packages in Python

A package is a hierarchical file directory structure, which defines a Python application environment composed of modules, subpackages, and subpackages under subpackages.

Simply put, a package is a folder, but there must be an init.py file under the folder , and the content of the file can be empty. init.py is used to identify the current folder as a package .

Python dependency introduction mechanism (search path), PYTHONPATH variable

When we import a module: import xxx , by default, the Python interpreter searches the module location in the following order: search the current directory, installed built-in modules, and third-party modules.

The module search path is stored in the sys.path variable of the system module . The variable contains the current directory, PYTHONPATH and the default directory determined by the installation process.

PYTHONPATH variable, to be accessed via:

>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

The order of the search path can also be changed when importing modules. There are two cases here:
1. Change by sys.path.append(), sys.path.insert() and other methods. When restarting the interpreter in this way , the original setting will be invalid.

2, Change PYTHONPATH, this setting method is permanently effective:

Using a python virtual environment, why should the virtual environment be activated

When using a Python virtual environment, it is recommended that you first activate the virtual environment to ensure that you are using the correct Python interpreter and related dependencies . If you do not execute the activate command, using the Python interpreter directly in the virtual environment may cause dependency issues or version conflicts .

/path/to/virtualenv/bin/python

This will directly start the Python interpreter in the virtual environment without activating the virtual environment. However, this practice is not recommended as it may cause dependency issues and version conflicts in the application, and it may be unstable.

When using virtual environments, the best practice is to activate the virtual environment before running your Python code. If you don't want to manually activate the virtual environment, you can add the path to the activate command to your shell configuration file (eg . Run Python programs in a virtual environment.

To activate the virtual environment, to activate the virtual environment is to temporarily configure the location of the current virtual environment python to the first place in the environment variable path. Since the command line searches the path from top to bottom in the environment variable path, if the first one is found, then It will not continue to search, and the python global environment will not be used.

We can view the activate virtualization environment script activate, there are operations such as setting PATH, PYTHONPATH, PATHHOME and so on.

Note: The python virtual environment does not necessarily have to be activated before you can run the python file

Guess you like

Origin blog.csdn.net/inthat/article/details/130416133