pip install -e安装成功但import失败:ModuleNotFoundError问题求助
I’ve run into this exact issue a few times with editable Python package installs, so let’s break down the most likely causes and fixes:
1. Mismatched Python/Pip Environments
This is the most common culprit. If you used pip install -e . but your pip is linked to a different Python interpreter than the one you’re using to run your script, the package won’t be visible.
- Fix steps:
- First, activate your virtual environment (if you’re using one) — it’s easy to forget this step!
- Reinstall using the Python interpreter you intend to use, to ensure alignment:
python -m pip install -e . - Verify the environment matches by checking the paths:
- On Windows:
where python where pip - On Linux/macOS:
which python which pip
- On Windows:
2. Incorrect setup.py Configuration
Your package might not be properly declared in setup.py, so even though pip reports a successful install, Python can’t locate the module.
- Key checks for your
setup.py:- Make sure you’re using
find_packages()to automatically detect all submodules (instead of manually listing them, which is error-prone). A minimal working setup for your structure would look like:from setuptools import setup, find_packages setup( name="gym-mabifish", version="0.0.1", packages=find_packages(), install_requires=["gym"], # Include any dependencies like OpenAI Gym # Add other necessary configs (entry_points, etc.) if needed ) - If you were manually specifying
packages, double-check that you includedgym_mabifishand all its subdirectories (likegym_mabifish.envs).
- Make sure you’re using
3. Verify the Package Path is in Python’s sys.path
Editable installs link your project directory to Python’s site-packages, but sometimes this link fails to register correctly.
- Check if your project path is recognized:
Run this command in the same environment where you’re getting the error:
Look for the pathpython -c "import sys; print('\n'.join(sys.path))"x:\path\to\project\gym-mabifishin the output. If it’s missing, the editable install didn’t add it — re-runningpython -m pip install -e .should fix this.
4. Double-Check IDE Interpreter Settings
If you’re using an IDE (like VS Code, PyCharm), make sure it’s configured to use the same Python environment where you installed the package. IDEs often default to system Python or a different virtual environment, which would cause the import error even if the command line works.
Once you’ve tried these steps, test the import again with a simple script:
import gym_mabifish print("Successfully imported!")
内容的提问来源于stack exchange,提问作者Runedog48

