Ubuntu20.04下Python3.5构建项目遇ImportError: No module named 'numpy'
Got it, let's work through this issue step by step. The core problem here is that numpy isn't installed for your custom Python 3.5 environment—even if it exists for your system's default Python 3.8, the two environments are completely separate. Here's how to fix this:
Step 1: Make sure Python 3.5 has its own pip
Custom Python installs sometimes don't set up pip automatically. Let's fix that first:
- Confirm your Python 3.5 path with:
which python3.5(you’ll likely see something like/usr/local/bin/python3.5) - Install or upgrade pip specifically for Python 3.5:
python3.5 -m ensurepip --upgrade
Step 2: Install numpy for Python 3.5 (not system Python)
Don’t use the generic pip install numpy command—it will install numpy for Python 3.8, not your 3.5 version. Instead, use Python 3.5's own pip:
python3.5 -m pip install numpy
If you hit compilation errors (since you’re using gcc-5), explicitly tell pip to use this compiler during numpy's build process:
CC=gcc-5 python3.5 -m pip install numpy
Step 3: Verify the installation
Double-check that numpy is now available for Python 3.5:
python3.5 -c "import numpy; print(numpy.__version__)"
If this prints a version number without errors, you’re ready to proceed.
Step 4: Re-run your build command
Now try executing your original command again:
python3.5 setup.py build develop
Pro Tip: Use a virtual environment (optional but highly recommended)
To avoid dependency conflicts between your system Python and custom Python 3.5, set up an isolated virtual environment:
- Install virtualenv for Python 3.5:
python3.5 -m pip install virtualenv - Create and activate a dedicated environment for your project:
virtualenv -p python3.5 my_project_env source my_project_env/bin/activate - Install numpy (and any other project dependencies) inside this environment, then run your setup command—this keeps everything clean and avoids cross-environment issues.
内容的提问来源于stack exchange,提问作者EhsanYaghoubi

