Fixing Python Package Installation Issues in aaPanel for Python 3.12
Introduction
While setting up a Django project on a VPS running Ubuntu 22.04 with aaPanel, I encountered errors during the installation of Python packages (gunicorn, uwsgi, and uvicorn) using the Python Manager plugin. The errors included:
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'
ERROR: Could not find a version that satisfies the requirement [package]
These issues stemmed from an outdated get-pip.py script used by aaPanel and an incompatible PyPI source. In this article, I’ll share the steps I took to resolve these issues, which should help others facing similar problems when deploying Python projects on aaPanel.
Problem Description
When attempting to install Python packages via the Python Manager in aaPanel, the following errors appeared in the logs:
Pip Installation Error:
Traceback (most recent call last):
File "/www/wwwroot/OV/09f------------------7_venv/get-pip.py", line 27071, in <module>
main()
...
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
Package Installation Failure:
ERROR: Could not find a version that satisfies the requirement gunicorn (from versions: none)
ERROR: No matching distribution found for gunicorn
These errors occurred because:
The Python Manager was downloading an outdated get-pip.py script (get-pip3.6.py) that was incompatible with Python 3.12.
The default PyPI source used by aaPanel might have had connectivity issues or lacked the required package wheels for Python 3.12.
Solution
To resolve the issue, I modified two key files in the aaPanel Python Manager plugin to update the get-pip.py download source and the PyPI repository used for package installations. Below are the steps I followed:
Step 1: Update the get-pip.py Download Source
The install_python.sh script in the Python Manager plugin was downloading an outdated get-pip.py script for Python 3.6, which caused compatibility issues with Python 3.12. I updated the script to use the official get-pip.py from bootstrap.pypa.io.
Locate the Script: The script is located at:
/www/server/panel/plugin/pythonmamager/install_python.sh
Edit the Script: Open the file with a text editor (e.g., nano):
sudo nano /www/server/panel/plugin/pythonmamager/install_python.sh
Modify the install_pip Function: Replace the following line in the install_pip function:
wget -O get-pip.py https://node.aapanel.com/install/plugin/pythonmamager/pip/get-pip${pyv:0:3}.py
with:
wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py
The updated install_pip function looks like this:
install_pip()
{
cd ${vpath}
wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py
if [ -f ${vpath}/bin/python ];then
bin/python get-pip.py
else
bin/python3 get-pip.py
fi
}
Save and Exit: Save the file (Ctrl+O, then Enter, and Ctrl+X to exit).
Test the Pip Installation: To ensure pip installs correctly, run the following commands in the virtual environment directory:
cd /www/server/python_manager/versions/3.12.0
wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py
bin/python3 get-pip.py
bin/pip --version
This should install a compatible version of pip for Python 3.12.
Step 2: Update the PyPI Source
The default PyPI source in aaPanel was causing issues with package downloads, possibly due to network restrictions or missing wheels for Python 3.12. I switched to a reliable PyPI mirror (Aliyun) to resolve this.
Locate the Python Manager Main File: The configuration file is located at:
/www/server/panel/plugin/pythonmamager/pythonmamager_main.py
Edit the File: Open the file with a text editor:
sudo nano /www/server/panel/plugin/pythonmamager/pythonmamager_main.py
Update the pipsource Variable: Find the line defining the pipsource variable:
pipsource = "https://mirrors.aliyun.com/pypi/simple/"
If it’s different or commented out, update it to:
pipsource = "https://mirrors.aliyun.com/pypi/simple/"
This ensures that the Python Manager uses the Aliyun PyPI mirror, which is fast and reliable.
Save and Exit: Save the file (Ctrl+O, then Enter, and Ctrl+X to exit).
Install Packages: Return to the aaPanel Python Manager and attempt to install the required packages (gunicorn, uwsgi, uvicorn) again. Alternatively, install them manually:
/www/server/python_manager/versions/3.12.0/bin/pip install gunicorn
Step 3: Install System Dependencies (Optional)
Some Python packages (e.g., uwsgi) require system-level dependencies for compilation. To avoid related errors, install the following packages:
sudo apt update
sudo apt install -y build-essential python3-dev libpcre3 libpcre3-dev libssl-dev zlib1g-dev
Step 4: Test the Django Project
After installing the packages, test your Django project with gunicorn:
cd /www/wwwroot/OV
source 09f4--------_venv/bin/activate
gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi:application
Access http://your_server_ip:8000 in a browser to verify that the project runs correctly.
Additional Recommendations
Backup Before Changes: Always back up the original install_python.sh and pythonmamager_main.py files before editing:
cp /www/server/panel/plugin/pythonmamager/install_python.sh /www/server/panel/plugin/pythonmamager/install_python.sh.bak
cp /www/server/panel/plugin/pythonmamager/pythonmamager_main.py /www/server/panel/plugin/pythonmamager/pythonmamager_main.py.bak
Update aaPanel: Ensure your aaPanel installation is up-to-date to benefit from the latest fixes:
bt
Select the update option.
Alternative PyPI Mirrors: If the Aliyun mirror doesn’t work due to regional restrictions, try other mirrors like:
pipsource = "https://pypi.tuna.tsinghua.edu.cn/simple/"
Python Version: If issues persist with Python 3.12, consider using Python 3.10 or 3.11, as some packages may not yet fully support Python 3.12.
Firewall Settings: Ensure ports 80, 443, and 8888 are open:
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 8888
Conclusion
By updating the get-pip.py download source to https://bootstrap.pypa.io/get-pip.py and switching the PyPI source to a reliable mirror (https://mirrors.aliyun.com/pypi/simple/), I successfully resolved the package installation issues in aaPanel’s Python Manager. This allowed me to install gunicorn and other dependencies for my Django project without errors.
I hope this guide helps others in the aaPanel community facing similar issues. If you have questions or encounter different errors, please share them in the thread, and I’ll do my best to assist!
Credits
Thanks to the aaPanel community for providing a great platform and to the open-source community for maintaining reliable PyPI mirrors.