简体   繁体   English

当我要从源代码运行和使用setuptools安装时都应使用什么导入系统?

[英]What import system should I use when I want to run my application both 'from source' and from installing with setuptools?

Consider this application: 考虑以下应用程序:

.
├── LICENSE
├── MANIFEST.in
├── program
│   ├── apple.py
│   ├── __init__.py
│   ├── __main__.py
│   ├── nonfruit.py
│   ├── pear.py
│   ├── strawberry.py
│   └── vegetables
│       ├── carrot.py
│       ├── __init__.py
│       └── lettuce.py
├── README.md
├── setup.cfg
└── setup.py

__main__.py is the file that users should run to use my program. __main__.py是用户应运行以使用我的程序的文件。 I am distributing my program via PyPI and so I want to be able to install it via pip as well. 我正在通过PyPI分发程序,因此我也希望能够通过pip进行安装。 Because of that, I created a setup.py file with an entry point: 因此,我创建了一个带有入口点的setup.py文件:

entry_points = {
      'console_scripts': ['pg=program.__main__:main']}

The problem I'm facing is that there are several imports in my program, and these result in the situation that my program does run 'locally' (by executing python ./__main__.py , but not from installation (by running pg ). Or, depending on the way I import it, the other way around. 我面临的问题是程序中有多个导入,这导致我的程序确实在本地运行(通过执行python ./__main__.py ,而不是从安装(通过运行pg )运行)。或者,取决于我导入它的方式,反之亦然。

__main__.py imports nonfruit.py : __main__.py导入nonfruit.py

from nonfruit import Nonfruit

nonfruit.py imports vegetables/carrot.py : nonfruit.py进口vegetables/carrot.py

import vegetables.carrot
ca = vegetables.carrot.Carrot()

I would like to hear some advice in structuring my project regarding imports, so that it runs both locally and from setuptools installation. 我想在构建有关导入的项目时听到一些建议,以便它既可以在本地运行,也可以从setuptools安装中运行。 For example, should I use absolute imports or relative imports? 例如,我应该使用绝对进口还是相对进口? And should I use from X import Y or import XY ? 我应该from X import Y还是from X import Y import XY

I found a solution on Jan-Philip Gehrcke's website . 我在Jan-Philip Gehrcke的网站上找到了解决方案。

The instructions are written for use with Python 3, but I applied it to Python 2.7 with success. 这些说明是为与Python 3结合使用而编写的,但是我成功地将其应用于Python 2.7。 The problem I was having originated from my directory becoming a package. 我的问题源于我的目录成为软件包。 Jan advices to create one file to run it from source ( bootstrap-runner.py ) and one file to run it from installation ( bootstrap/__main__.py ). Jan建议创建一个文件来从源代码运行它( bootstrap-runner.py ),并创建一个文件来从安装bootstrap/__main__.py运行它( bootstrap/__main__.py )。 Furthermore, he advices to use explicit relative imports: 此外,他建议使用显式相对导入:

from .X import Y

This will probably be a good guideline in the next applications I'm writing. 在我正在编写的下一个应用程序中,这可能是一个很好的指南。

暂无
暂无

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

相关问题 如果我使用 python 运行系统命令,并想得到它的动态结果,我该怎么办? - If I use python to run a system command, and want to get it's dynamic result, what should i do? 安装Gensim时出错:“无法导入从源代码发行版安装所需的setuptools。” - Error installing Gensim: “Could not import setuptools which is required to install from a source distribution.” 我应该创建一个包含所有模块导入的文件,然后从中导入*,还是应该将模块导入到我使用的每个文件中? - Should I create a file with all my module imports and then import * from that, or should I import the modules to each file I use? Python 用setuptools打包安装时不包含我的源代码 - Python packaging with setuptools does not include my source code when installing 我应该安装什么Distribute或Setuptools - What should I install Distribute or Setuptools 当我想使用pyinstaller打包我的python脚本时,如果我想在我的脚本中使用“pygame.font.SysFont”怎么办? - when I want to use pyinstaller to package my python script, what should I do if I want to use "pygame.font.SysFont" in my script? 我什么时候应该(不)想在我的代码中使用 pandas apply() ? - When should I (not) want to use pandas apply() in my code? 我的Facebook应用程序应使用哪种授权 - What kind of authorization I should use for my facebook application Setuptools:使用源文件夹中的setup.py从“项目”导入 - Setuptools: Import from 'project' with setup.py in source folder 当我想继续使用我的计算器时如何从头开始循环。 在我使用它之后,如果我输入 9,我希望它关闭 - how to loop from the start when i want to keep using my calculator. and after i use it i want it to close if i type 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM