我构建了一个python包并上传到pypi。它安装得很好,但在导入时我会收到ModuleNotFound
错误。该错误在全新的conda环境中复制,在不同的机器上(Ubuntu、MacOS、Windows。尽管都在(新的(conda-envs中(。我上一次出现这个错误是因为文件夹的名称不正确,但这里的情况并非如此。GitHub repo位于分支和用于构建whl的文件夹中。
有什么想法吗?
包是使用构建和安装的
python3 -m build
python3 -m twine upload dist/*
pip install pillaralgos
(pillar_env) jupyter-pomkos@jupyterubuntu:~$ python
Python 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
---------------------------------------------------------------------------
>>> import pillaralgos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pillaralgos'
>>>
它显示在pip list
中
(pillar_env) jupyter-pomkos@jupyterubuntu:~$ pip list
Package Version
------------------ -------------------
# Truncated
pexpect 4.8.0
pickleshare 0.7.5
pillaralgos 1.0.1
Pillow 8.2.0
pip 21.0.1
sys.path
确实包含库安装所在的目录。
(pillar_env) jupyter-pomkos@jupyterubuntu:~$ python
Python 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/jupyter-pomkos/.conda/envs/pillar_env/lib/python38.zip', '/home/jupyter-pomkos/.conda/envs/pillar_env/lib/python3.8', '/home/jupyter-pomkos/.conda/envs/pillar_env/lib/python3.8/lib-dynload', '/home/jupyter-pomkos/.local/lib/python3.8/site-packages', '/home/jupyter-pomkos/.conda/envs/pillar_env/lib/python3.8/site-packages']
>>>
pip show
表示它安装在正确的环境和目录中。
(pillar_env) jupyter-pomkos@jupyterubuntu:~$ pip show pillaralgos
---------------------------------------------------------------------------
Name: pillaralgos
Version: 1.0.1
Summary: Algorithms for Pillar. Currently includes "mini" algorithms, nothing too sophisticated.
Home-page: https://github.com/pillargg/twitch_chat_analysis/tree/pypi_reorganize
Author: Peter Gates
Author-email: pgate89@gmail.com
License: UNKNOWN
Location: /home/jupyter-pomkos/.conda/envs/pillar_env/lib/python3.8/site-packages
Requires:
Required-by:
Note: you may need to restart the kernel to use updated packages.
ModuleNotFoundError
出现在jupyter控制台、jupyterlab笔记本和终端python中。确认内核指向正确的conda目录(尽管我认为没有必要进行故障排除,因为错误在不同的机器上复制(:
(pillar_env) jupyter-pomkos@jupyterubuntu:~$ nano ~/.local/share/jupyter/kernels/pillar_env/kernel.json
---------------------------------------------------------------------------
{
"argv": [
"/home/jupyter-pomkos/.conda/envs/pillar_env/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Pillar Env",
"language": "python"
}
您当前的setup.cfg
如下所示:
[options]
package_dir =
= pillaralgos
packages = find:
# ...
[options.packages.find]
where = pillaralgos
将其与您的项目目录结构进行比较,在我看来,您应该更改为以下内容:
[options]
packages = find:
# ...
如果我没有错的话,package_dir
字段以及整个[options.packages.find]
部分都不是必需的。
对于所有的包构建/发布,切换到poetry
,他们似乎头脑清醒。
据说是用python内置的python3 -m build
和python twine upload dist/*
命令运行的:
- 确保本地和github上的所有文件夹中都有
__init__.py
- 确保文件夹结构正确<---这就是问题所在
默认python命令的文件夹结构应为:
|-- pypi
|-- src # <---- this folder can be named pillaralgos, but still needed another pillaralgos subfolder
|-- pillaralgos # <----- this is needed
|-- helpers
|-- __init__.py
|-- data_handler.py
|-- __init__.py
|-- algoXX.py
|-- LICENSE
|-- pyproject.toml
|-- README.md
|-- setup.cfg
poetry
开箱即用。文件夹结构:
|-- pypi
|-- pillaralgos # <---- note that poetry didn't require an additional subfolder
|-- helpers
|-- __init__.py
|-- data_handler.py
|-- graph_helpers.py
|-- sanity_checks.py
|-- __init__.py # must include version number
|-- algoXX.py # all algorithms in separate files
|-- LICENSE
|-- README.md
|-- pyproject.toml # must include version number