简体   繁体   English

我应该如何为此设置 python 包

[英]How should I set up the python packages for this

I am creating a tool I plan to put on Github.我正在创建一个我计划放在 Github 上的工具。 It is very general however I want to also include some examples of its usage.它非常笼统,但是我还想包括一些使用示例。 As such I have set up the following folder structure.因此,我设置了以下文件夹结构。

This tool is for python 3.此工具适用于 python 3。

repository/
   commonTool.py
   commonTool2.py
   specificUsage/
     runTheSpecificUsage.py
     helpRunTheSpecificUsage.py

Now both of the scripts in the specificUsage/ folder will import the methods in commonTool.py and commonTool2.py.现在, specificUsage/文件夹中的两个脚本都将导入 commonTool.py 和 commonTool2.py 中的方法。

My issue is ideally the user would be able to go我的问题是理想情况下用户将能够 go

python repository/specificUsage/runTheSpecificUsage.py

However I cant get this to work.但是我不能让它工作。 It never is able to import the functions that are in the folder above it.它永远无法导入其上方文件夹中的功能。 I have tried a variety of various posts on how to import a file from a super folder with no luck.我已经尝试了各种关于如何从超级文件夹中导入文件但没有运气的帖子。

How should I be setting up these folders?我应该如何设置这些文件夹? Should I have one init .py or two?我应该有一个init .py 还是两个? Where?在哪里?

You can create a setup.py at the top level along with commonTool.py and commonTool2.py .您可以在顶层创建setup.py以及commonTool.pycommonTool2.py Inside your setup.py, put the following line:在 setup.py 中,输入以下行:

import setuptools
setuptools.setup(packages=setuptools.find_packages())

Then put an __init__.py in the same level, as well as in specificUsage/ .然后将__init__.py放在同一级别以及specificUsage/中。 You should be able to import from specificUsage like this:您应该能够像这样从specificUsage导入:

import commonTool

After setting up your files, from the top level run:设置文件后,从顶层运行:

pip install -e .

You may also want to consider that Python has strong naming conventions for modules and packages and frowns upon camelcase in general.您可能还想考虑 Python 对模块和包具有很强的命名约定,并且通常不赞成驼峰式。 You can read more here if you like.如果您愿意,可以在这里阅读更多内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM