我构建了一个 python 包并上传到 pypi。可以安装但不能导入:ModuleNotFoundError

I built a python package and uploaded to pypi. It can be installed but not imported: ModuleNotFoundError

我构建了一个 python 包并上传到分支上的 pypi. It installs just fine, however on import I get ModuleNotFound error. The error was replicated in brand new conda environments, on different machines (Ubuntu, MacOS, Windows. Although all in (new) conda envs). The last time I had this error was because the folder was incorrectly named, however that is not the case here. 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 notebook 和终端中 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] 部分都不是必需的。

所有包 building/publishing 都切换到 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