【问题标题】:Can't find my python module after installing on Github Actions在 Github Actions 上安装后找不到我的 python 模块
【发布时间】:2021-03-08 21:42:05
【问题描述】:

当我在本地环境中安装我的示例模块时,python 能够在导入模块时找到它。 然而,当由 Github Actions 执行时,工作流失败并且报告的错误是我的模块(ci-test)没有安装。

main.yaml:

  - name: Install ci-test package
    run: |
      python setup.py build
      python setup.py install 
      python -c "import ci_test"

完整的 yaml 文件位于 here。 Github Actions 的错误输出是:

Installed /home/runner/.local/lib/python3.7/site-packages/ci_test-0.0.1-py3.7.egg
Processing dependencies for ci-test==0.0.1
Finished processing dependencies for ci-test==0.0.1
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'ci_test'
Error: Process completed with exit code 1.

【问题讨论】:

    标签: python github-actions


    【解决方案1】:

    该问题与 github 操作无关。

    查看您的存储库时,存储库是这样组织的。

    ci-test/
    |-- requirements.txt
    |-- setup.py
    |-- src/
    |   |-- ci_test/
    |   |   |-- app.py
    |   |   |-- __init__.py
    |   |   |-- main.py
    |-- tests/
    |   |-- app_test.py
    |   |-- __init__.py
    |   |-- main_test.py
    

    在您的setup.py 中,您将您的包裹描述为:

    packages=['src/ci_test', 'tests']
    

    您的ci_test 包可以通过以下路径导入:import src.ci_test

    这个问题有一些关于python项目结构的最佳实践:What is the best project structure for a Python application?

    【讨论】:

    • 你说得对,错误在于我的项目结构。我无法将测试模块与主包分开,我通过在主模块中包含测试包解决了这个问题,现在它可以完美运行了。谢谢你的帮助,你给了我一个很好的方向,我会用你的意见重新表述这个问题。
    【解决方案2】:

    抱歉,我找不到任何其他相关问题:

    如果安装后运行:

    pip3 install --user -r requirements.txt

    你仍然得到一个例外:外部库的ModuleNotFoundError: No module named(在我的情况下是jsonrpcserver)即使它存在于requirements.txt中,你直接使用pip3 install命令安装它,试试theese:

    • 删除预定义的 github 操作安装 python 的方式,从此:
        - name: Set up Python ${{ matrix.python-version }}
          uses: actions/setup-python@v2
          with:
            python-version: ${{ matrix.python-version }}
    

    对此:sudo apt-get install python3

    • 尝试从pip install 命令中删除--user 标志

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2016-05-29
      相关资源
      最近更新 更多