简体   繁体   English

可安装的distutils软件包:如何从也是同一软件包一部分的命令行脚本中导入软件包模块?

[英]Installable distutils packages: How do I import package modules from command line scripts which are also part of the same package?

I've created a Python package that contains both pure modules and scripts, all within the same folder. 我创建了一个Python包,其中包含纯模块和脚本,它们都在同一文件夹中。 The scripts need to make use of functionality within the modules so simply import them: 这些脚本需要利用模块中的功能,因此只需导入它们即可:

import *module*

I now wish to share this package with others so I've turned it into a distributable package using distutils. 我现在希望与其他人共享此软件包,因此我已使用distutils将其转换为可分发的软件包。 As instructed by the documentation for distutils, I've declared the script files within my package in 'setup.py' as follows: 按照distutils 文档的说明,我已在包中的“ setup.py”中声明了脚本文件,如下所示:

setup(...,
    scripts=['path/to/script/a', 'path/to/script/b']
    )

After installing this package, I notice that distutils has installed a copy of my command line scripts to the 'Scripts' folder within my Python installation. 安装此软件包后,我注意到distutils已将命令行脚本的副本安装到Python安装中的“ Scripts”文件夹中。 All well and good. 一切都很好。 Now, if I try an run any one of these scripts, it fails with: 现在,如果我尝试运行这些脚本中的任何一个,它将失败并显示:

ImportError: No module named *module*

Presumably this is because the relative path between the scripts and the modules has changed after installation so it can no longer find the modules. 大概是因为安装后脚本和模块之间的相对路径已更改,因此无法再找到模块。 So my question is, how are you supposed to import modules from scripts within the same package so that it works both pre-bundling with distutils and post-installation? 所以我的问题是,您应该如何从同一软件包中的脚本中导入模块,以使其既可以与distutils捆绑在一起,又可以在安装后使用?

Now I could easily solve this by modifying my module import like so: 现在,我可以通过修改模块导入来轻松解决此问题,如下所示:

try:
    import *module*
except ImportError:
    from *package* import *module*

This seems like a bit of a hack. 这似乎有点hack。 Am I missing a trick here? 我在这里错过了一个把戏吗? I would have expected distutils to take care of this for me. 我希望distutils可以帮我解决这个问题。 Is there a better and more robust way of handling this? 有没有更好,更强大的方法来处理此问题?

Maybe try setuptools - it has a nice way to automatically create main script . 也许尝试setuptools它有一个很好的自动创建主脚本的方法 Short example: 简短示例:

setup(
    # other arguments here...
    entry_points = {
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ],
        'gui_scripts': [
            'baz = my_package_gui.start_func',
        ]
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打包一个接受命令行参数并且还具有依赖关系的 Python 脚本? - How do I package a single python script which takes command line arguments and also has dependencies? 在python中,如何从distutils包中公开一些命令? - In python, how do I expose some command from my distutils package? 如何找到可从 package 中导入的模块? - How do I find the modules that are available for import from within a package? 如果我将一个包导入到 python 中,我是否还必须单独重视它的模块? - If I import a package into python, do I also have to important it's modules separately? 如何打包 python 应用程序以使其可安装? - How do I package a python application to make it pip-installable? 如何从distutils源码包创建Python鸡蛋? - How do I create Python eggs from distutils source packages? 如何在不使用distutils的情况下创建一个debian软件包来分发python模块? - how can I create a debian package to distribute python modules without using distutils? 如何从包内“主”模块导入包模块 - How to import package modules from in-package “main” module 如何从包的__init__.py导入包模块? - How to import package modules from package's __init__.py? 如何从一个包中导入文件,而该包又从同一包中导入另一个文件 - How to import a file from a package which is importing another file from the same package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM