【问题标题】:ModuleNotFoundError: No module named <modulename> after installing custom package with pipModuleNotFoundError:使用 pip 安装自定义包后没有名为 <modulename> 的模块
【发布时间】:2021-10-18 20:42:52
【问题描述】:

在问这个问题之前,我已经看到以下问题与我的问题有些相似,但它们并没有解决现有的错误:

我有以下结构:

backbone-project
├── backbone
│   ├── backbone.py
│   ├── cmd_parser.py
│   ├── command_executor.py
│   ├── config_files
│   ├── db_connections
│   ├── extras
│   ├── __init__.py
│   ├── __pycache__
│   ├── query_files
│   ├── templates
│   ├── tests
│   └── utils
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py

这是我的 setup.py 文件:

import setuptools
from backbone.utils.enums import Files, Letters


with open(Files.README.value, Letters.R.value, encoding=Letters.UTF_8.value) as readme_file:
    long_description = readme_file.read()

with open(Files.REQUIREMENTS.value, Letters.R.value, encoding=Letters.UTF_8.value) as requirements_file:
    requirements = requirements_file.read().splitlines()

setuptools.setup(
    name="backbone",
    version="1",
    entry_points={
      "console_scripts": [
          'backbone=backbone.backbone:main'
      ]
    },
    author="Mostafa Ghadimi",
    author_email="my email",
    long_description=long_description,
    install_requirements=requirements,
    # install_requires=requirements,
    long_description_content_type="text/markdown",
    url="url",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir={"": "backbone"},
    packages=setuptools.find_packages("backbone"),
    python_requires=">=3.6",
    license='MIT',
)

backbone/backbone.py

#!/usr/bin/env python
from cmd_parser import parse_args
from utils.logger import Logger
from utils.enums import Messages


def main():
    logger = Logger(__name__)
    logger.log(Messages.BACKBONE_WELCOME_MSG.value)
    parse_args()


if __name__ == '__main__':
    main()

问题是,每当我要安装包时,它都安装成功,但是在执行命令时,出现错误:

requirements.txt

git+https://<username>:<password>@<git_repository_url>.git

并使用以下命令安装它:

pip install -r requirements

(已经安装,没有任何错误。)

由于我想使用包(在 bash 中),我面临以下错误:

Traceback (most recent call last):
  File "/home/user/.local/bin/backbone", line 5, in <module>
    from backbone import main
ImportError: cannot import name 'main' from 'backbone' (unknown location)

我该如何解决这个错误?

附注1在本地导出PYTHONPATH后问题解决了,但是当我想从git仓库安装包时问题仍然存在!

附注2 我以某种方式确定问题出在entry_points。我也试过backbone:main,但是不行!

【问题讨论】:

  • 我发现了很多奇怪的东西...但首先,我认为您不需要 setup.py 中的 package_dir 行。那么它应该是install_requires=...(而不是install_requirements)。
  • @sinoroc 你介意帮我解决现有的问题吗?
  • 我不知道。太多的事情似乎是错误的。也许试试这个:在你的 requirements.txt 你应该写类似 backbone @ git+https://... 的东西。
  • @sinoroc 我试过这个,但它不起作用。我以某种方式确定问题出在entry_points,但我不知道如何解决它。我会在一分钟内添加我的backbone.py 文件。
  • 在你的入口点你提到backbone.main:main,但你似乎没有任何backbone.main模块(或包),换句话说没有backbone/main.py(或backbone/main/__init__.py) .

标签: python pip


【解决方案1】:

原因是已经存在名为'backbone'的包。所以它从 pip 安装它。但是您需要的是本地模块,也称为“主干”。要解决这个问题,只需键入“.backbone”(在模块名称前添加点)而不是“backbone”。

【讨论】:

  • 感谢您的回答,请您提一下我应该应用更改吗?您介意编辑答案吗?
  • @MostafaGhadimi 更改应该在 setup.py 文件的顶部,您可以从“backbone”导入“main”。此外,我建议删除您安装的模块,以免冲突。
  • 对于本地命令使用.backbone 未修复。除了别名,还有什么方法可以将其命名为骨干网?
猜你喜欢
  • 2020-04-05
  • 2019-11-07
  • 2021-06-26
  • 2022-07-30
  • 2017-08-26
  • 2022-01-03
  • 2020-06-15
  • 2020-08-08
  • 2020-10-12
相关资源
最近更新 更多