[Python] The difference between python -m pip install and pip install

1. pip install

When you use the pip install command, you are using the Python package manager pip to install Python packages or modules. The following is a detailed understanding of pip install:

  1. Install Package: pip install <package>The command is used to install the specified Python package. You need to provide the name of the package or the identifier of the package to install. For example, to install a package called requests, you would do pip install requests. pip will download the version of the package from the Python Package Index (PyPI) and install it into your Python environment.
  2. Package dependencies: pip is able to resolve package dependencies and automatically install other packages that a package depends on. When you install a package, pip will check for other packages that the package depends on and make sure those dependencies are also installed. This ensures that all dependencies required for your project or application to work properly are met.
  3. Package versioning: pip allows you to specify versions of packages to install. You can use operators such as equals (=), greater than (>), and less than (<) to specify a range of versions, or to specify a specific version number directly. For example, pip install requests==2.25.1 will install the requests package with specific version number 2.25.1.
  4. Install from a file: In addition to directly specifying the package name for installation, you can also batch install packages by specifying a text file containing a list of dependent packages. This file is usually called "requirements.txt" and it lists all the packages required by the project along with their version numbers. You can execute pip install -r requirements.txt command to install these packages.
  5. Install from: By default, pip downloads packages from the Python Package Index (PyPI). But you can also specify other sources, such as local file paths, Git repositories, or other remote package indexes. This enables you to install packages from different sources.
  6. Virtual environment support: pip supports virtual environments, which can create independent Python environments for each project. This ensures that the packages and versions used by each project are isolated from each other, avoiding conflicts between packages.

Summarize:

pip install is the command to install Python packages using the pip package manager. It can download and install packages from the Python Package Index, handle package dependencies, support version control, support installation from files, and specify different installation sources. This makes installing and managing Python packages convenient and flexible.

Two, python -m pip install

When you use the python -m pip install command, you are running the pip module with the -m argument to the Python interpreter, thereby installing a Python package or module. The following is a detailed understanding of python -m pip install:

  1. Specify the Python interpreter: python -m is a command-line option that tells the operating system to use the Python interpreter to execute the specified module. This ensures that the correct Python interpreter is used to execute the corresponding module, independent of default settings or environment variables on the system. By using the -m parameter, you can explicitly specify which Python interpreter provides the module to run.
  2. Use the pip module: pip is Python's package manager for installing, upgrading, and managing Python packages. You can use the Python interpreter's built-in pip functionality to install packages by adding the pip module after python -m. This usage is useful for ensuring proper use of pip in different Python environments.
  3. Install package: Similar to the pip install command, python -m pip install <package>the command is used to install the specified Python package. You need to provide the name of the package or the identifier of the package to install. For example, to install a package called requests, you would execute python -m pip install requests. This will use the Python interpreter's built-in pip module to download and install the package into the current Python environment.
  4. Package Manager Features: python -m pip provides the full functionality of the pip package manager, you can use various subcommands and options to perform other operations such as upgrading packages, uninstalling packages, listing installed packages, etc. By adding the corresponding subcommand after python -m pip, you can perform specific package management operations.
  5. Virtual environment support: Similar to pip install, python -m pip install also supports virtual environments. You can run the python -m pip install command in a specific virtual environment to ensure that the specified package is installed in that environment.

Summarize:

The python -m pip install command uses the -m parameter of the Python interpreter to run the built-in pip module to install Python packages. It can correctly use pip in different Python environments, and provides complete package management functions, including installation, upgrade, uninstallation and other operations. This usage is useful to ensure that the correct Python interpreter and environment are used to perform package management operations.

3. Summary of the two

  1. pip install <package>: This is the usual way of installing Python packages directly using the pip command. Input on the command line pip install <package>, pip will install the specified package in the current Python environment. This usage assumes that the system has a properly configured pip executable path and is able to find and execute pip commands directly.
  2. python -m pip install <package>: This way of writing uses the -m parameter to call the pip module. It is to ensure that a specific Python module can be found and executed exactly without conflicting with other executable commands or scripts on the system. By using the -m parameter, you can explicitly tell the system to use the Python interpreter to execute the pip module, independent of the system's environment variable settings. This usage avoids potential conflicts or errors, especially when using pip across multiple Python versions or environments.

Summarize:

  • pip install <package>It is a common way to install Python packages, assuming that the system has correctly configured the pip executable path.
  • python -m pip install <package>The -m parameter is used to ensure that pip is run as a Python module, which is suitable for ensuring that pip is used correctly in different environments.

Guess you like

Origin blog.csdn.net/wzk4869/article/details/131554126