使用pyproject.toml配置TradingPlatform项目时出现‘package directory 'TradingPlatform' does not exist’错误的求助
pyproject.toml配置TradingPlatform项目时出现‘package directory 'TradingPlatform' does not exist’错误的求助
Problem
在运行单元测试时,我尝试从绝对路径导入模块,遇到了如下错误:
tests/unit/test_parameter_combinations.py:3: in <module> from TradingPlatform.core.parameter_combinations import ParameterCombinations E ModuleNotFoundError: No module named 'TradingPlatform'
ChatGPT建议如果要打包项目,可以创建可编辑安装来解决路径问题,而我之后也打算发布这个项目,所以我创建了如下toml文件:
[build-system] requires = ["setuptools>=64", "wheel"] build-backend = "setuptools.build_meta" [project] name = "TradingPlatform" version = "0.1.0" description = "A platform for trading infrastructure and strategy testing." dependencies = [ "pandas", "numpy", "scikit-learn", "statsmodels", ] [tool.setuptools] packages = ["TradingPlatform"] [tools.setuptools.packages.find] where = ["."] include = ["TradingPlatform*"]
这是简化后的项目目录结构(我移除了core和tests里的部分模块来精简展示):
TradingPlatform ├── __init__.py ├── core │ ├── __init__.py │ ├── base_trading_strategy.py ├── notebooks │ └── main.ipynb ├── pyproject.toml ├── tests │ ├── integration │ │ └── __init__.py │ └── unit │ ├── __init__.py │ └── test_strategy_performance_evaluation.py └── utils ├── __init__.py ├── helper_funcs.py └── logging_setup.py
但现在我遇到了错误:error: package directory 'TradingPlatform' does not exist。
What I Have Tried
我被这个错误困扰了很久,尝试过重新配置toml文件、卸载并重新安装包,但都没用。以下是我尝试过的几种toml配置:
[tool.setuptools] packages = ["TradingPlatform", "TradingPlatform.core", "TradingPlatform.utils", "TradingPlatform.tests"] [tool.setuptools.package-dir] # Explicitly tell setuptools where to find the root package directory TradingPlatform = "TradingPlatform"
[tool.setuptools] packages = ["TradingPlatform", "TradingPlatform.core", "TradingPlatform.utils", "TradingPlatform.tests"]
[tool.setuptools.packages.find] where = ["."]
我原本期望在.egg-info目录下的top_level.txt文件中能找到目录名TradingPlatform以及各个子模块:
core tests utils
但实际上文件里只有TradingPlatform,有时候甚至是空的。
我对打包发布项目不太熟悉,非常希望能得到解决这个问题的指导。如果需要更多细节,请告诉我。
备注:内容来源于stack exchange,提问作者InvestingScientist
相关产品推荐
相关产品推荐

