Ubuntu 18.04+Python3.7.3导入pandas报_lzma模块未找到错误求助
Hey there, let's work through this tricky _lzma module issue you're hitting with pandas. Even though _lzma isn't listed as a direct pandas dependency, it's likely being pulled in indirectly—either by a pandas sub-dependency or when pandas tries to handle compressed file formats like .xz. Here's how to resolve it:
Step 1: Ensure System-Level LZMA Libraries Are Installed
Python's _lzma module relies on the system's liblzma-dev package. Even if you think it's installed, re-installing it ensures all dependencies are intact:
sudo apt-get update && sudo apt-get install --reinstall liblzma-dev
Step 2: Verify Python Can Access the _lzma Module
First, check if the module path is in Python's system path. Fire up a Python 3.7 shell and run:
import sys print("/usr/lib/python3.7/lib-dynload" in sys.path)
If this returns False, you can add the path temporarily (for the current session) with:
sys.path.append("/usr/lib/python3.7/lib-dynload")
To make this permanent, add the following line to your ~/.bashrc file:
export PYTHONPATH="$PYTHONPATH:/usr/lib/python3.7/lib-dynload"
Then reload your shell with source ~/.bashrc.
Step 3: Recompile Python 3.7.3 (If Needed)
If the above steps don't work, the problem might be that your Python 3.7.3 was compiled without linking to the system LZMA libraries. Here's how to recompile it properly:
- Download the Python 3.7.3 source code (if you don't already have it):
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz tar xzf Python-3.7.3.tgz cd Python-3.7.3 - Configure the build to explicitly include LZMA support:
./configure --with-lzma - Compile and install (use
altinstallto avoid overwriting your system's default Python):sudo make && sudo make altinstall
Step 4: Reinstall a Python 3.7-Compatible Pandas Version
Sometimes version mismatches can cause odd dependency issues. Try installing a pandas version that's confirmed to work with Python 3.7:
pip3 uninstall -y pandas pip3 install pandas==1.3.5
Why This Happens
Even though _lzma isn't a direct pandas dependency, pandas uses Python's standard library lzma module under the hood when working with compressed files. If your Python build didn't properly link to the system's LZMA libraries during compilation, the module file might exist but fail to load when called.
内容的提问来源于stack exchange,提问作者Marco Dalla Gasperina

