在Python中使用setuptools安装时,如何使导入的文件在控制台脚本中工作?

问题描述 投票:0回答:1

通常我在Python中创建控制台脚本,但不使用setup tools.现在我试图使用setuptools自动管理本地安装,并使其为分发做好准备,我遇到了一个可能有点普通的问题,但在Google中找不到任何东西。python3 my_script.py,而且有 imports 但是当我用setuptools安装时,我得到了以下错误。

ModuleNotFoundError: No module named 'My_Object'

...... 如果我尝试在本地导入一个叫做 My_Object.

setuptools 是不是也应该安装模块的依赖关系来使脚本工作?或者是安装文件中缺少了什么?

以下是我的 setup.py 项目根目录下的文件内容。

from setuptools import setup

VERSION = '0.0.1'

def readme():
    with open('README.md') as f:
        return f.read()

setup(
    name='object-test',
    version=VERSION,
    description="Test setup script",
    long_description_content_type="text/markdown",
    long_description=readme(),
    keywords="Testing import in installation",
    author="Danilo Silva",
    author_email="[email protected]",
    packages=["src"],
    entry_points={"console_scripts": ["importtest=src.entry:main"],},
    include_package_data=True
)

下面,内容来自 entry.py 文件在我的本地 src 文件夹。

from My_Object import My_Object

def main():
    print("Hello! This is just an application test for importing")
    my_object = My_Object()
    print(my_object.main())

# main()

My_Object.py 文件,也在 src 文件夹。

class My_Object:

    def main(self):
        return 'Hello! And this is the imported output!'

一般来说,我都会取消最后一个文件夹的注释。main()entry.py 只是为了检查一切是否正常,然后运行脚本,在控制台中键入 python3 entry.py. 这确保了我的脚本中一切正常,没有任何的 ModuleNotFoundError 被抛出。

如何使导入语句在setuptools安装的控制台脚本中工作?我缺少什么?

python pip setuptools
1个回答
1
投票

从我所看到的,我相信第一行的内容是 "我"。src/entry.py 文件应读。

from src.My_Object import My_Object

由于它的设置。src顶层 包,其中包含 My_Object 模块。

© www.soinside.com 2019 - 2024. All rights reserved.