You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用setup.py构建模块后无法导入函数,site-packages中calc.py为空

Python包安装后无法导入函数,site-packages中calc.py为空的问题

错误信息

运行测试代码时出现以下导入错误:

Traceback (most recent call last):
  File "/home/thoma/PycharmProjects/test_gymnasium/test.py", line 1, in <module>
    from test_final_1 import my_add
ImportError: cannot import name 'my_add' from 'test_final_1' (/home/thoma/anaconda3/envs/test_gymnasium/lib/python3.8/site-packages/test_final_1/__init__.py)

项目目录结构

├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── test_final_1
│           ├── calc.py
│           └── __init__.py
├── setup.py
├── test_final_1
│   ├── calc.py
│   └── __init__.py
└── test_final_1.egg-info
    ├── dependency_links.txt
    ├── PKG-INFO
    ├── requires.txt
    ├── SOURCES.txt
    └── top_level.txt

各文件内容

init.py

from test_final_1.calc import *

calc.py

def my_add(a,b):
    return a+b

def my_sub(a,b):
    return a-b

test.py

from test_final_1 import my_add
a = 2
b = 4
my_add(2,4)

setup.py

from setuptools import setup

setup(
    name="test_final_1",
    version="0.0.1",
    install_requires=[],
)

操作步骤

在项目父目录执行以下命令安装包:

pip install -e .

补充说明(编辑1)

查看site-packages目录下的包文件,发现__init__.py内容正常,但calc.py文件是空的,请问这是什么原因?

内容的提问来源于stack exchange,提问作者desert_ranger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:01:25