我正在打包我的第一个Python项目,我有两个带有辅助函数的文件,comments.py和score.py存储在子目录中。
YT_Sentiments/
└── src/
├── YT_Sentiments/
│ ├── helpers/
│ │ ├── __init__.py
│ │ ├── score.py
│ │ └── comments.py
│ ├── __init__.py
│ └── main.py
├── LICENSE
├── pyproject.toml
├── README.md
└── setup.py
在我的 main.py 中导入两个文件
from helpers import score
from helpers import comments
当我在本地构建并安装它时,出现错误:
File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\YT_Sentiments\main.py", line
1, in <module>
from helpers import score
ModuleNotFoundError: No module named 'helpers'
我已经遵循了至少 4 个有关打包的教程,并实现了我认为可以解决我的问题的内容(setup.py 和 pyproject.toml)
设置.py
from setuptools import find_packages, setup
with open("README.md") as f:
long_description = f.read()
setup(name = "YT_Sentiments",
version = "0.0.4",
description = "analyze comments from any youtube video!",
package_dir = {"YT_Sentiments":"YT_Sentiments",
"YT_Sentiments.helpers":"YT_Sentiments/helpers",},
packages = ['YT_Sentiments', 'YT_Sentiments.helpers'],
long_description=long_description,
long_description_content_type="text/markdown",
license = "MIT",
classifiers=[
"Programming Language :: Python",
"Topic :: Software Development :: Libraries :: Python Modules",
"Operating System :: OS Independent",
],
install_requires = ["emoji>=2.12.1",
"google_api_python_client>=2.139.0",
"vaderSentiment>=3.3.2",
"vaderSentiment>=3.3.2",],
python_requires = ">=3.8",
)
pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "YT_Sentiments"
version = "0.0.4"
description = "A python package that allows you to perform sentiment analysis on Youtube comments"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"emoji>=2.12.1",
"google_api_python_client>=2.139.0",
"vaderSentiment>=3.3.2",
"vaderSentiment>=3.3.2",
]
[project.urls]
Homepage = "https://github.com/pypa/sampleproject"
Issues = "https://github.com/pypa/sampleproject/issues"`
我认为这个问题的原因是你的终端不在主文件夹中。你能试试这个吗?
from src.YT_Sentiments.helpers import score
from src.YT_Sentiments.helpers import comments