(airformer) C:\Users\lenovo\Documents\WeChat Files\wxid_i2lj4bj8hh2v22\FileStorage\File\2025-07\SeaFormer\SeaFormer>pip install numpy scipy Requirement already satisfied: numpy in c:\users\lenovo\.conda\envs\airformer\lib\site-packages (1.24.3) Collecting scipy Using cached scipy-1.10.1-cp38-cp38-win_amd64.whl.metadata (58 kB) Using cached scipy-1.10.1-cp38-cp38-win_amd64.whl (42.2 MB) Installing collected packages: scipy Successfully installed scipy-1.10.1 (airformer) C:\Users\lenovo\Documents\WeChat Files\wxid_i2lj4bj8hh2v22\FileStorage\File\2025-07\SeaFormer\SeaFormer>python ./experiments/airformer/main.py --mode train --gpu 0 --dataset AIR_TINY Traceback (most recent call last): File "./experiments/airformer/main.py", line 14, in <module> from src.utils.helper import get_dataloader, check_device, get_num_nodes ModuleNotFoundError: No module named 'src' (airformer) C:\Users\lenovo\Documents\WeChat Files\wxid_i2lj4bj8hh2v22\FileStorage\File\2025-07\SeaFormer\SeaFormer>pip install src Collecting src Downloading src-0.0.7.zip (6.3 kB) Preparing metadata (setup.py) ... done Building wheels for collected packages: src Building wheel for src (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [60 lines of output] C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\dist.py:261: UserWarning: Unknown distribution option: 'tests_require' warnings.warn(msg) running bdist_wheel running build running build_py creating build\lib\src copying src\__init__.py -> build\lib\src running egg_info writing src.egg-info\PKG-INFO writing dependency_links to src.egg-info\dependency_links.txt deleting src.egg-info\entry_points.txt writing requirements to src.egg-info\requires.txt writing top-level names to src.egg-info\top_level.txt reading manifest file 'src.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' adding license file 'LICENSE.rst' writing manifest file 'src.egg-info\SOURCES.txt' C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\dist.py:932: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! command.initialize_options() Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "C:\Users\lenovo\AppData\Local\Temp\pip-install-lf17u9jc\src_eb337413b1b94c31abdd9318b6b38b79\setup.py", line 70, in <module> setup( File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\__init__.py", line 117, in setup return distutils.core.setup(**attrs) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\core.py", line 183, in setup return run_commands(dist) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\core.py", line 199, in run_commands dist.run_commands() File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\dist.py", line 954, in run_commands self.run_command(cmd) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\dist.py", line 999, in run_command super().run_command(command) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\dist.py", line 973, in run_command cmd_obj.run() File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\command\bdist_wheel.py", line 412, in run install = self.reinitialize_command("install", reinit_subcommands=True) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\__init__.py", line 227, in reinitialize_command cmd = _Command.reinitialize_command(self, command, reinit_subcommands) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\cmd.py", line 309, in reinitialize_command return self.distribution.reinitialize_command(command, reinit_subcommands) File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\dist.py", line 938, in reinitialize_command for sub in command.get_sub_commands(): File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\cmd.py", line 327, in get_sub_commands if method is None or method(self): File "C:\Users\lenovo\.conda\envs\airformer\lib\site-packages\setuptools\_distutils\command\install.py", line 785, in has_lib self.distribution.has_pure_modules() or self.distribution.has_ext_modules() AttributeError: 'NoneType' object has no attribute 'has_pure_modules' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for src Running setup.py clean for src Failed to build src ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (src)
时间: 2025-07-22 17:27:41 浏览: 3
<think>我们正在解决一个Python导入错误:ModuleNotFoundError: No module named 'src'
根据引用内容,问题通常出现在项目结构中使用相对导入或绝对路径设置不正确时。
引用[1]指出:在命令行运行时,应该在src同级目录(项目根目录)运行,或者修改代码添加路径。
引用[2]说明:IDE会自动将项目根目录加入PYTHONPATH,但命令行不会,因此需要手动添加。
引用[3]和[4]提供了解决方案:在代码中添加sys.path.append来将项目根目录加入模块搜索路径。
解决方案:
1. 确保在项目根目录下运行脚本(即src的父目录)。
2. 如果不在根目录运行,可以在代码中动态添加项目根目录到sys.path。
具体步骤:
方法一:改变运行目录
假设项目结构为:
project/
src/
module1.py
main.py
在main.py中可能有:from src.module1 import something
此时,应该在project目录下运行:python main.py
方法二:修改代码(在运行文件开头添加路径)
在运行文件(比如main.py)的开头添加以下代码:
```python
import os
import sys
# 获取当前文件所在目录的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取项目根目录(假设当前文件在项目根目录下,或者需要向上退一级等)
# 例如,如果当前文件在project/src/main.py,那么项目根目录是current_dir的父目录的父目录?
# 这里需要根据实际情况调整,通常我们希望将项目根目录(即包含src的目录)加入sys.path
# 假设运行文件在项目根目录,那么直接使用current_dir即可,但若运行文件在子目录,则需要向上层追加。
# 常见做法:将当前文件的上级目录(即项目根目录)加入sys.path
# 如果运行文件就在项目根目录,那么父目录就是项目根目录。
project_root = os.path.dirname(current_dir) # 如果运行文件在项目根目录,则current_dir就是根目录,那么它的父目录就不是我们想要的。所以这里需要根据情况调整。
# 更通用的做法:如果运行文件在项目根目录下的某个子目录(如src),我们想将根目录加入路径,可以:
# 方法:获取当前目录的父目录(即项目根目录)并加入sys.path,但前提是运行文件在子目录中。
# 但如果我们希望无论运行文件在何处,都将其所在的项目根目录加入,则可能需要确定一个基准(比如通过寻找某个标志性文件或目录)。
# 根据引用[4]的做法:使用两个os.path.dirname向上退两级(因为__file__是当前文件,第一个dirname得到当前文件所在目录,再往上退一级就是该目录的父目录)
# 例如,如果当前文件在project/src/main.py,那么:
# os.path.abspath(__file__) -> project/src/main.py的绝对路径
# os.path.dirname(__file__) -> project/src
# os.path.dirname(os.path.dirname(__file__)) -> project
# 所以,我们可以将项目根目录(project)加入sys.path:
# 因此,在运行文件开头添加:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# 但是注意:如果运行文件就在项目根目录下,那么os.path.dirname(os.path.abspath(__file__))就是根目录,再取一次dirname就退到了项目根目录的上级,这就不对了。
# 所以,更合理的做法是:根据项目的结构,确定需要添加的路径。通常,我们只需要将项目根目录(即包含src目录的目录)加入sys.path。
# 引用[4]的解决方案是:在需要导入src模块的文件开头添加:
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# 这样,无论这个文件在项目中的哪个位置,都会将它的祖父目录(即项目根目录)加入sys.path。
# 但注意:如果项目结构更深,可能需要调整层数。
# 另一种做法:直接获取项目根目录的绝对路径并加入,比如:
# project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# sys.path.append(project_root)
# 或者,使用:
# sys.path.insert(0, os.path.abspath('..')) # 将上级目录加入模块搜索路径的最前面
# 具体使用哪种方法取决于项目结构。
# 推荐做法:在运行文件的开头,将项目根目录的绝对路径加入sys.path。
# 假设项目结构为:
# project/
# src/
# __init__.py
# module1.py
# main.py (运行文件)
# 那么,在main.py中,我们只需要将project目录(即当前文件所在目录)加入sys.path?实际上,当运行main.py时,当前目录(project)已经在sys.path中,所以可以直接import src.module1。
# 但是,如果我们在project目录下运行python src/main.py,那么当前目录是project,而main.py在src目录下,这时sys.path的第一个路径是src的父目录(即project),所以也能导入src。
# 因此,问题通常出现在:在src目录内运行一个脚本,而这个脚本试图导入同目录的模块(作为包的一部分)时,由于当前目录(src)不在sys.path中,或者没有将项目根目录加入,导致无法以顶级包的形式导入。
# 总结:如果运行文件在子目录中,并且需要导入兄弟目录的模块,那么需要将项目根目录加入sys.path。
# 所以,建议在运行文件开头添加:
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) # 将上级目录(项目根目录)加入
# 这样,无论运行文件在项目根目录还是子目录,都能正确导入。
# 注意:如果运行文件就在项目根目录,那么'..'会指向父目录(可能不是项目根目录,而是项目根目录的上级),所以这种方法要求运行文件在项目根目录下的某个子目录中。
# 因此,更通用的做法是:在运行文件中,将项目根目录的绝对路径加入sys.path。可以通过寻找一个已知文件(如README.md)来确定,但通常我们采用相对路径调整。
# 鉴于上述复杂性,最可靠的方法是在项目根目录运行,或者使用IDE设置工作目录。
方法三:使用IDE,设置工作目录为项目根目录。
方法四:设置PYTHONPATH环境变量。
在运行前,将项目根目录添加到PYTHONPATH中。
例如,在Linux/macOS中:
export PYTHONPATH=/path/to/project
在Windows中:
set PYTHONPATH=C:\path\to\project
根据引用[1]和[4],添加路径的代码示例(在运行文件中):
```python
import os
import sys
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, cur_path) # 将当前目录加入,但这样可能不够,因为当前目录可能是子目录
# 或者,使用两个os.path.dirname来获取项目根目录(假设当前文件在src/util中,那么两次dirname后得到项目根目录)
root_path = os.path.dirname(os.path.dirname(cur_path))
sys.path.insert(0, root_path)
```
但注意:如果项目结构复杂,可能需要根据实际情况调整层数。
因此,针对用户的问题,我们建议:
1. 检查项目结构,确保在正确的目录下运行脚本(项目根目录)。
2. 如果必须从子目录运行,则在运行文件开头添加路径设置代码,将项目根目录加入sys.path。
示例代码(在运行文件开头添加):
```python
import sys
import os
# 获取当前文件的目录,并向上退一级得到项目根目录(假设运行文件在项目根目录下的某个子目录中)
base_dir = os.path.dirname(os.path.abspath(__file__))
base_dir = os.path.dirname(base_dir) # 退到上一级,即项目根目录
sys.path.insert(0, base_dir)
```
或者,如果运行文件就在项目根目录下,那么直接使用当前目录即可(通常不需要额外添加,因为运行文件所在目录会被自动加入sys.path)。所以问题通常发生在运行文件在子目录中时。
因此,请用户根据自己项目结构调整路径添加的层数。
注意:避免使用相对导入(如from .. import module)而使用绝对导入(如from src.module import something),并确保项目根目录在sys.path中。
最后,如果用户的项目是一个包,建议使用setuptools进行安装,这样可以在任何位置使用。
但是,对于开发阶段,上述方法更快捷。</think>### 解决 ModuleNotFoundError: No module named 'src'
在 Python 项目中遇到 `ModuleNotFoundError: No module named 'src'` 错误,通常是由于**项目结构路径问题**导致 Python 无法正确识别模块导入路径。以下是系统化的解决方案:
---
### 解决方案分步说明
#### 方法 1:在项目根目录运行脚本(推荐)
1. **定位项目根目录**
找到包含 `src/` 文件夹的顶层目录(项目根目录),例如:
```
your_project/ 👈 项目根目录
├── src/ # 代码目录
│ └── main.py # 主程序
└── requirements.txt
```
2. **在根目录执行命令**
打开终端,切换到项目根目录后运行:
```bash
cd /path/to/your_project # 进入根目录
python -m src.main # 通过模块方式运行
```
#### 方法 2:修改代码添加路径(动态解决方案)
在报错文件的开头添加以下代码(以 `main.py` 为例):
```python
import os
import sys
# 获取当前文件的绝对路径,并向上回退到项目根目录
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(current_dir) # 回退到 src 的上级目录
# 将项目根目录加入 Python 搜索路径
sys.path.insert(0, project_root)
# 现在可以正常导入 src 下的模块
from src.utils import helper # 示例导入
```
#### 方法 3:设置环境变量(永久生效)
- **Linux/macOS**
在终端执行:
```bash
echo "export PYTHONPATH=\"/path/to/your_project\"" >> ~/.bashrc
source ~/.bashrc
```
- **Windows**
命令提示符执行:
```cmd
setx PYTHONPATH "C:\path\to\your_project"
```
---
### 根本原因分析
- **IDE vs 命令行差异**
IDE(如 PyCharm)会自动将项目根目录加入 `sys.path`,而命令行不会[^2]。
- **相对导入失效**
当在子目录(如 `src/`)中直接运行脚本时,Python 无法回溯到父目录寻找模块[^4]。
- **包结构未声明**
若 `src/` 是一个 Python 包,需确保其包含 `__init__.py` 文件(可为空文件)。
---
### 预防措施
1. **标准化项目结构**
参考官方建议的布局:
```
project_root/
├── src/ # 源码目录
│ ├── __init__.py
│ └── module.py
├── tests/ # 测试目录
├── setup.py # 安装脚本
└── README.md
```
2. **始终从根目录运行**
使用 `python -m src.module` 替代 `python src/module.py`[^1]。
3. **使用虚拟环境**
通过 `venv` 隔离环境,避免路径冲突:
```bash
python -m venv venv
source venv/bin/activate # Linux/macOS
venv\Scripts\activate # Windows
```
---
### 常见问题解答
**Q1: 为什么 PyCharm 能运行,命令行报错?**
A1: PyCharm 自动将项目根目录加入 `sys.path`,命令行需手动配置[^2]。
**Q2: 已添加 `sys.path` 仍报错?**
A2: 检查路径层级:
```python
# 错误:回退层级不足(仍在 src/ 内)
sys.path.append(os.path.dirname(__file__))
# 正确:回退到项目根目录
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
```
**Q3: 如何检查当前 `sys.path`?**
A3: 在代码中添加:
```python
import sys
print(sys.path) # 查看所有搜索路径
```
---
阅读全文
相关推荐



















