Featured image of post Specifying Domestic Sources for Module Installation Using pip in Python

Specifying Domestic Sources for Module Installation Using pip in Python

pip is the package manager for Python, which simplifies the installation and deployment of Python programs. With pip, users can easily...

pip is the package manager for Python, designed to simplify the installation and deployment of Python programs. With pip, users can easily download and install Python modules. By default, pip fetches modules from the Python Package Index (PyPI). However, in some situations—especially in China where downloads are typically slow (and sometimes don’t work at all)—users may need to specify alternative sources for downloading modules. This article will guide you on how to specify sources for module installation using pip in Python.

Step 1: Check Available Sources

Open your command line terminal and enter the following command to view the currently available sources:

1
pip config list

This command will list all the option names defined in the current pip configuration file along with their corresponding values.

Step 2: Select and Add a Source

Choose the source you wish to use and add it to your configuration file. For example, if you want to use the Tsinghua University mirror, you can add it to the configuration file with the following command (if you haven’t created a pip configuration file before, it will be created automatically):

1
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

This sets https://pypi.tuna.tsinghua.edu.cn/simple as the global default source. If you only want to use this source for the current installation, you can omit the --global parameter.

Step 3: Install Modules

You can now install the required modules using the pip install command, and pip will automatically download and install the modules from the specified source:

1
pip install <module_name>

If you wish to use a different source for a specific project, you can add the source URL to that project’s configuration file (e.g., .pip/pip.conf file).

By following these steps, you can specify sources for module installation using pip in Python. Please note that if the specified source is unavailable or fails to download the required modules, you may need to switch to another available source.

I hope this article is helpful to you.

Licensed under CC BY-NC-SA 4.0