Home >Backend Development >Python Tutorial >How to specify source installation with pip
Pip specified source installation has three methods: "specify the source in the command line", "configure the default source" and "use environment variables": 1. Use the --index-url or -i parameter in the command line Specify the source; 2. Create a pip.conf file in the pip folder in the user directory and add the URL address of the default source; 3. Set the "PIP_INDEX_URL" environment variable to specify the URL of the source.
# Operating system for this tutorial: Windows 10 system, Dell G3 computer.
To specify a specific source to install a Python package, you can use the --index-url or -i parameter to specify the URL of the source. The following are several common ways to specify the source:
pip install --index-url <源的URL> <包名>
or
pip install -i <源的URL> <包名>
For example, to use Tsinghua University To install the requests package from the university's mirror source, you can execute the following command:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
[global] index-url = <源的URL>
Replace
For example, on Windows, you can create a pip.ini file in the C:\Users\your username\pip directory and add the following content:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple
In this way, you Will default to using the specified source for all pip installations.
Using environment variables:
Another method is to specify the source by setting an environment variable. The PIP_INDEX_URL environment variable can be set to specify the URL of the source.
In the command line, use the following syntax to set environment variables and install packages:
PIP_INDEX_URL=<源的URL> pip install <包名>
For example, to use the mirror source of Tsinghua University to install the requests package, you can Execute the following command:
PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple pip install requests
One of these methods should allow you to specify a specific source to install Python packages from. Keep in mind that when using unofficial sources, make sure the source is trustworthy and you still need to be careful with installed packages.
The above is the detailed content of How to specify source installation with pip. For more information, please follow other related articles on the PHP Chinese website!