Python 2.7提示No module named yum,求针对该环境编译yum的指导
Hey there, let’s work through compiling the yum module specifically for your Python 2.7 installation on Oracle Linux. I’ve gone through similar headaches before, so here’s a practical step-by-step guide to get this sorted:
Step 1: Install Compilation Dependencies
First, make sure you have all the tools and libraries needed to build yum from source. Run this command to install the required packages:
sudo yum install gcc python27-devel rpm-devel openssl-devel libxml2-devel libxslt-devel yum-utils
The yum-utils package gives us yumdownloader, which we’ll use to grab the yum source code.
Step 2: Retrieve YUM Source Code
Download the yum source RPM package for your Oracle Linux version:
yumdownloader --source yum
Once downloaded, extract the source files from the RPM:
rpm2cpio yum-*.src.rpm | cpio -idmv
This will create a directory with all the yum source files and build configurations.
Step 3: Adjust Configurations for Python 2.7
Navigate into the extracted source directory. We need to tweak a few files to make yum target your Python 2.7 installation:
Modify the yum.spec file:
Openyum.specin a text editor, and look for lines defining Python paths. Update%define python_siteliband%define python_sitearchto point to your Python 2.7’s site-packages directory. For 64-bit systems, this is usually:%define python_sitelib /usr/lib64/python2.7/site-packages %define python_sitearch /usr/lib64/python2.7/site-packagesFor 32-bit systems, replace
lib64withlib.Update shebang lines:
Check all Python scripts in the source (likeyum,yum-config-manager, etc.) and change their shebang from#!/usr/bin/pythonto#!/usr/bin/python2.7. This ensures they use your target Python version instead of the system default.
Step 4: Build and Install YUM for Python 2.7
Before building, make sure your Python 2.7 has setuptools installed. If not, run:
sudo easy_install-2.7 setuptools
Then, build and install the yum module:
cd path/to/yum-source-directory python2.7 setup.py build sudo python2.7 setup.py install
Step 5: Verify the Installation
Test if the yum module is now accessible in Python 2.7:
python2.7 -c "import yum; print('YUM module version:', yum.__version__)"
If this outputs the yum version without errors, you’re good to go!
Important Notes
- Don’t overwrite the system yum: Your Oracle Linux system likely relies on the default Python version (often 2.6) for system yum operations. Keep the system yum intact, and use your compiled version only for your Python 2.7 scripts.
- Dependency checks: If you run into import errors after installation, make sure Python 2.7 has all required dependencies like
rpm,pycurl, andlibxml2installed. You can install them viapip2.7if available, or via yum packages built for Python 2.7.
内容的提问来源于stack exchange,提问作者carte blanche

