我希望能够执行trump并使用rest api获得随机报价。
注意:王牌之前没有python

我有以下目录结构-

trump
|--setup.py
|--trump
   |-- quote.py
   |-- __init__.py
   |-- __main__.py


setup.py包含-

from setuptools import setup, find_packages
import sys

if sys.version_info[0] != 3:
    raise RuntimeError('Unsupported python version "{0}"'.format(sys.version_info[0]))

setup(name='trump',
      version='0.1',
      description='Trumps tweets',
      keywords=['trump'],
      #include_package_data=True,
      packages=find_packages('trump'),
      package_dir={'':'trump'},
      license='GNU GPL v3',
      python_requires="!=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
      install_requires=[
          'requests',
      ],
      entry_points = {
            'console_scripts': [
                'trump = trump.__main__:main'
          ]
      },
      zip_safe=False
      )


__init__.py为空。
__main__.py包含-

import quote
def main():
    quote.vomit()

if __name__=='__main__':
    main()


并且quote.py包含-

import requests

def vomit():
    api_response = requests.get('http://api.tronalddump.io/random/quote')
    quote = api_response.json()['value']

    print(quote)


当我运行python3 trump时,在根目录中,我得到正确的结果。但是,当我通过pip将其安装为pip3 install --user -e .并尝试执行trump时,出现以下错误-

 $ trump
Traceback (most recent call last):
  File "/home/walker/.local/bin/trump", line 11, in <module>
    load_entry_point('trump', 'console_scripts', 'trump')()
  File "/home/walker/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 487, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/walker/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2728, in load_entry_point
    return ep.load()
  File "/home/walker/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2346, in load
    return self.resolve()
  File "/home/walker/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2352, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'trump'


我尝试遵循python packaging docs,但似乎无法弄清为什么会这样。

编辑:
完成packages=find_packages()后,出现以下错误-

$ pip install --user -e .
Obtaining file:///trump
    Complete output from command python setup.py egg_info:
    running egg_info
    writing trump/trump.egg-info/PKG-INFO
    writing dependency_links to trump/trump.egg-info/dependency_links.txt
    writing entry points to trump/trump.egg-info/entry_points.txt
    writing requirements to trump/trump.egg-info/requires.txt
    writing top-level names to trump/trump.egg-info/top_level.txt
    error: package directory 'trump/trump' does not exist

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /trump/


这使我相信package_dirpackages的配置错误。但是问题是,如果我在package_dir行中添加注释,并且在package的任何一种情况下(都为王号或为空),我都会得到以下响应-

$ trump
bash: trump: command not found

最佳答案

'trump'删除packages=find_packages('trump')'trump'是要包含在发行版中的软件包,而find_packages('trump')trump/目录中查找,但在该目录中未找到任何软件包并返回空列表。

packages=find_packages()


另外,您根本不需要package_dir。如果您在其中有trump/子目录(例如src/),则可以使用它,

package_dir = {'':'src'},

关于python - 尝试创建应该用作命令行脚本的自定义程序包时出现ModuleNotFoundError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53879186/

10-13 09:43