【问题标题】:I built a python package and uploaded to pypi. It can be installed but not imported: ModuleNotFoundError我构建了一个python包并上传到pypi。可以安装但不能导入:ModuleNotFoundError
【发布时间】:2021-07-06 03:46:28
【问题描述】:

我构建了一个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 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"
}

【问题讨论】:

    标签: setuptools python-packaging


    【解决方案1】:

    您当前的setup.cfg 如下所示:

    [options]
    package_dir =
        = pillaralgos
    packages = find:
    # ...
    
    [options.packages.find]
    where = pillaralgos
    

    将其与您的项目目录结构进行比较,在我看来您应该更改为以下内容:

    [options]
    packages = find:
    # ...
    

    如果我没记错的话,package_dir 字段以及整个[options.packages.find] 部分都不是必需的。

    【讨论】:

    • 我继续使用poetry(作为答案发布),但测试了您的建议,它确实有效!所以我假设package_dir = pillaralgos 将在已经存在的pillaralgos 目录中定义一个子目录,这就是为什么我的新文件夹结构使用默认的python3 -m build 命令。对吗?
    • 是的。 package_dir 用于顶级包不在setup.py 旁边而是在另一个目录中(通常是src 目录)的情况。
    【解决方案2】:

    所有包构建/发布都切换到poetry,他们似乎正直奔。

    说要使用 python 的内置 python3 -m buildpython 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 2021-05-06
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多