【发布时间】:2021-10-18 20:42:52
【问题描述】:
在问这个问题之前,我已经看到以下问题与我的问题有些相似,但它们并没有解决现有的错误:
- ModuleNotFoundError: No module named after pip install
- ImportError: No module… After python setup.py install
我有以下结构:
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) .