In Python development, virtual environments and dependency management are essential tools. However, in actual operation, we often encounter various dependency conflicts, missing modules or incompatibility. This article will use a practical case as a basis to analyze in detail how to solve the dependency problem in the Python virtual environment, and finally achieve the smooth operation of the project by downgrading the Python version.
1. Problem background
When developing an e-commerce project, I encountered the following problem: When using Python 3.13 to create a virtual environment and install the dependency package on Windows system, the pip install -r command reported an error, prompting ModuleNotFoundError: No module named 'distutils'. The following are the specific error messages:
ERROR: Exception:
Traceback (most recent call last):
File "D:\Desktop\Graduation Design\E-commerce-Li Xiaoyan\project\venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 105, in _run_wrapper
status = _inner_run()
...
ModuleNotFoundError: No module named 'distutils'
After analysis, it was found that the root cause of the problem is the lack of the distutils module, and the deeper reason is the compatibility issue between the Python version and the dependency package.
2. Problem analysis
2.1 What is distutils
distutils is a standard library module in Python that is used to build and install Python packages. It played a major role in earlier Python versions, but in Python 3.12 and later, distutils was marked as deprecated and may be removed. Therefore, if your project depends on certain packages that require distutils, you may encounter similar problems.
2.2 Compatibility between dependency package and Python version
By analyzing the files, I found that the packages the project depends on have certain requirements for the Python version. The following are the versions of the main dependency package and their requirements for the Python version:
Dependency package | Version | Supported Python versions |
---|---|---|
Flask | 2.2.2 | 3.7+ |
Flask-SQLAlchemy | 3.0.2 | 3.7+ |
pandas | 1.5.1 | 3.8+ |
numpy | 1.23.4 | 3.8+ |
SQLAlchemy | 1.4.44 | 3.6+ |
Werkzeug | 2.2.2 | 3.7+ |
As can be seen from the table, most dependencies require that Python versions are at least 3.7, while pandas and numpy require that Python versions are at least 3.8. Therefore, Python 3.8 or 3.9 is a more suitable choice.
3. Solution
3.1 Install the distutils module
First, I tried to solve the problem by installing the distutils module. On Windows systems, you can use the following command:
python -m ensurepip --default-pip python -m pip install --upgrade pip setuptools
However, this approach did not work in Python 3.13, because distutils has been removed.
3.2 Use setuptools instead of distutils
Now that distutils has been deprecated, I tried using setuptools as a replacement. Install setuptools with the following command:
pip install setuptools
However, the problem persists because some dependencies still depend on distutils.
3.3 Downgrade Python version
After further analysis, I decided to downgrade the Python version to 3.8 or 3.9 to ensure that all dependencies are installed and run properly. The following are the specific steps:
3.3.1 Uninstall the current Python version
1. Open the control panel, find Python, and select Uninstall.
2. Or use the command line to uninstall (if installed through the package manager):
winget uninstall Python
3.3.2 Download and install Python 3.8 or 3.9
accessPython official download page。
Download the Python 3. or 3. installation package.
During installation, make sure to check the "Add Python to PATH" option.
3.3.3 Verify installation
Open the terminal and run the following command to check the Python version:
python --version
Make sure the output is Python 3. or Python 3.
3.3.4 Recreate the virtual environment
1. Delete the existing virtual environment (if any):
rm -r venv
2. Create a new virtual environment:
python -m venv venv
3. Activate the virtual environment:
Windows:
venv\Scripts\activate
macOS/Linux:
source venv/bin/activate
3.3.5 Reinstalling the dependency package
Run in a virtual environment:
pip install -r
4. Result verification
After the above steps, all dependency packages are successfully installed and the project can run normally. Here is an example of the output after the installation is completed:
Successfully installed Flask-2.2.2 Flask-SQLAlchemy-3.0.2 Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.2.2 click-8.1.3 colorama-0.4.6 greenlet-2.0.1 importlib-metadata-5.1.0 itsdangerous-2.1.2 numpy-1.23.4 pandas-1.5.1 python-dateutil-2.8.2 pytz-2022.6 six-1.16.0 zipp-3.11.0
5. Summary and Suggestions
Through the solution to this problem, I have summarized the following experiences:
Python version selection:
- When developing new projects, try to choose a stable Python version (such as 3.8 or 3.9) to avoid compatibility issues.
- For old projects, you need to select the appropriate Python version based on the dependency package in the file.
Virtual environment management:
- Always install dependency packages in the virtual environment to avoid contaminating the global environment.
- When switching Python versions, remember to recreate the virtual environment.
Dependency package compatibility:
- When installing dependency packages, be careful to check the range of Python versions it supports.
- If you encounter problems, you can try to install dependent packages one by one to troubleshoot which package is causing the problem.
Tool usage:
- Use the latest versions of pip and setuptools to ensure better compatibility and feature support.
- For complex dependency management, you can consider using tools such as poetry or pipenv.
6. Reference code
Here are the main commands and code snippets used in this article:
6.1 Install distutils and setuptools
python -m ensurepip --default-pip python -m pip install --upgrade pip setuptools
6.2 Create and activate a virtual environment
# Create a virtual environmentpython -m venv venv # Activate the virtual environment (Windows)venv\Scripts\activate # Activate the virtual environment (macOS/Linux)source venv/bin/activate
6.3 Installing dependency packages
pip install -r
6.4 Check Python version
python --version
Through the detailed analysis and solutions of this article, I hope that readers can better understand common problems in Python virtual environment and dependency management, and master the methods to solve these problems. If you encounter similar problems during development, you may wish to refer to the steps in this article for troubleshooting and solving them.
This is the end of this article about the detailed explanation of the solution to the virtual environment dependency problem in Python. For more related content on Python virtual environment dependency, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!