Python package management and module import guide
When writing Python, package management and module import are two things that you deal with every day. If used well, the environment will be clean and the code will be clear; if used incorrectly, you will frequently run intoModuleNotFoundError, and even fell into the situation of "this machine can run, but that machine reports an error". This guide will use official recommendations in a way that novices can understand.pip, virtual environments, alternativescondaGo over the behind-the-scenes rules of module import to help you avoid common pitfalls.
1. Official first choice: pip package management tool
1.1 Why pip?
pipIt is Python's official package management tool. It mainly downloads, installs, upgrades, and uninstalls various packages from PyPI (Python Package Index, the world's largest Python third-party package warehouse).
The good news is: Python 3.4 and above versions have built-in pip, so there is no need to run it manually.get-pip.py. If you accidentally lost it or the version is too old, you can fix it with the following command:
1.2 Quick check of commonly used commands
The following commands cover 80% of usage scenarios. Note: All commands are usedpython -m pipform, why this is more secure will be explained later.
1.3 What should I do if multiple versions of Python coexist?
Do you have Python 3.8, 3.10, or even Python 2.7 installed on your computer? Please forget to type directlypip、pip3For this kind of vague command, using "target interpreter -m pip" is the most reliable way:
By doing this, you will always know which Python environment you are packaging into.
2. Artifact for isolating dependencies: virtual environment
2.1 Why use a virtual environment?
Let's say you maintain two projects at the same time:
- Project A depends on
requests==2.25(Requires running under Python 3.7) - Project B dependencies
requests==3.x(Requires Python 3.12)
If all packages are installed in the global environment, conflicts will inevitably occur. Virtual environment is to give each project an independent Python sandbox, and install dependencies separately without interfering with each other.
2.2 Python native venv (recommended for lightweight projects)
Python 3.3 or above has built-invenvModule, no additional installation required:
2.3 Correct usage of requirements.txt
pip freezeWill list all packages in the current environment (including indirect dependencies), suitable for deployment/release scenarios. However, in the development stage, it is recommended to only write the core packages that the project directly depends on, which is clearer and easier to maintain:
This can avoid unnecessary changes due to minor version changes in indirect dependencies, and at the same time cooperate withpip install -r requirements.txtAll indirect dependencies will still be automatically resolved and installed.
3. Alternative for data science players: Anaconda
If you are mainly engaged in data analysis and machine learning, you will definitely have heard of Anaconda. It is pre-installed with 1500+ commonly used scientific computing packages such as NumPy, Pandas, Matplotlib, etc.condaThe tool can not only manage Python packages, but also different versions of Python interpreters and C/C++ libraries.
3.1 Installation suggestions
- It is recommended to install Miniconda (contains only Python and conda, small size and high flexibility), and then manually install the scientific computing package as needed.
- Installation package can be downloaded from Anaconda 官网 graphical installer.
- Windows users: Although checking "Add Anaconda to PATH" is convenient for novices to use directly, it may conflict with other software; advanced users can leave it unchecked and activate conda through Anaconda Prompt or manually.
3.2 conda high-frequency commands
4. Behind-the-scenes rules for module import: search path
Sometimes the package is clearly installed, but the import error messageModuleNotFoundError, most likely because Python cannot find the installation location of the package.
4.1 View the current search path
You can see this by running a simple Python code:
When Python imports a module, it will search in these paths from top to bottom, and will stop once it finds a matching module.
4.2 How to add a path temporarily or permanently
If your custom modules are not in the project root directory, there are two ways to get Python to find them.
Method 1: Temporarily add during runtime (only valid for the current script/interactive window)
Method 2: Add permanently (valid for all Python sessions)
By setting environment variablesPYTHONPATHaccomplish:
5. Pitfall avoidance guide: Solving common problems
5.1 What should I do if the installation fails?
- Network Timeout: Domestic users recommend configuring Tsinghua, Alibaba and other mirror sources, which are very fast.
- Insufficient permissions: Windows runs the terminal as administrator; Unix/macOS can add
--userInstall to the user directory (but a virtual environment is more recommended). - Compilation error: Some packages depend on C extensions, Windows can install Visual Studio Build Tools; Unix/macOS installs gcc/clang; or install precompiled binary packages directly through conda.
5.2 Three-step troubleshooting of import errors
- Check whether the package is installed:
python -m pip show package_name - Check the case carefully: Python is case-sensitive.
import Requestswill fail. - Confirm that the package installation location is
sys.pathMedium: Print path list for manual comparison.
6. Best practices that novices must follow
- One virtual environment for each project, fundamentally eliminating dependency conflicts.
- use
python -m piprather than nakedpip, to avoid the confusion of multiple versions. - Domestic users can configure permanent mirror sources, which greatly improves the installation experience.
- Maintain concise requirements.txt during development and reuse it during release
pip freezeGenerate complete dependency files. - Do not submit the virtual environment folder to Git - in
.gitignoreAdd inmyenv/(or your environment name).
By mastering these, you can easily control Python's package management and module import, and keep the development environment clean and efficient~

