如何避免我的代码的用户键入冗余导入行? [英] How do I avoid users of my code having to type redundant import lines?

查看:189
本文介绍了如何避免我的代码的用户键入冗余导入行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为Pants的项目,住在GitHub.com。最初该项目是一个名为 pants.py 的单个 .py 文件。

 裤子/ 
pants.py
README.md



,用户可以如下导入和使用:

  import pants 
w = pants.World()






这两个都对我很好。然后我读了如何构建一个项目,包括单元测试,并最初重组我的项目:

 裤子/ 
pants.py
README.md
test /
__init__.py
test_world.py
test_ant.py

这样做的问题是,虽然用户仍然可以导入相同的逻辑方式,但没有 pants.test module / package!没问题,我想,我会简单地添加另一个 __ init __。py 文件:

 裤子/ 
__init__.py
pants.py
README.md
test /
__init__.py
test_world.py
test_ant.py

但现在导入感觉令人难以置信的重复:

  import Pants.pants 
w = Pants.pants.World()






它只是觉得有一个更好的方法!现在,我的项目结构如下:

 裤子/ 
README.md
pants /
__init__.py
ants.py
world.py
solver.py
test /
__init__.py
test_world.py
test_ant.py

然而,用户面临的导入行同样重复:

  import pants.world 
import pants.solver
w = pants.world.World()
s = pants .solver.Solver()






可以将这些东西别名缩短等同物,例如 import pants.world.World as World ,但是导入行本身仍然感觉重复。关于如何避免这种重复,同时保留适当的项目结构的任何建议?如果我要通过 pip

解决方案

为了解决它,我保持我的包结构相同,并添加以下行到 pants / __ init __。py

 来自.ant import Ant 
来自.world import World
来自.solver import Solver

然后我可以将我的演示文件顶部的导入行更改为:

  from pants import World 
from pants import Solver


So I have a project called "Pants" that lives on GitHub.com. Originally the project was a single .py file called pants.py.

Pants/
    pants.py
    README.md

and users could import and use it as follows:

import pants
w = pants.World()


Both of these feel fine to me. Then I read up on how to structure a project with unit tests included, and initially reorganized my project thusly:

Pants/
    pants.py
    README.md
    test/
        __init__.py
        test_world.py
        test_ant.py

The problem with this is that although users can still import the same logical way, there is no pants.test module/package! No problem, I thought, I'll simply add another __init__.py file:

Pants/
    __init__.py
    pants.py
    README.md
    test/
        __init__.py
        test_world.py
        test_ant.py

But now the imports feel incredibly repetitive:

import Pants.pants
w = Pants.pants.World()


It just feels like there is a better way! Right now, my project is structured like this:

Pants/
    README.md
    pants/
        __init__.py
        ants.py
        world.py
        solver.py
        test/
            __init__.py
            test_world.py
            test_ant.py

However, the import lines users are faced with are equally repetitive:

import pants.world
import pants.solver
w = pants.world.World()
s = pants.solver.Solver()


Now I know you can alias these things to shorter equivalents, such as import pants.world.World as World but the import line itself still feels repetitive. Any suggestions on how to avoid such repetition while retaining proper project structure? Does any of this have to change if I were to, say, put it up for installation via pip?

解决方案

To fix it, I kept my package structure the same, and added the following lines to pants/__init__.py:

from .ant import Ant
from .world import World
from .solver import Solver

Then I was able to change the import lines at the top of my demo file to:

from pants import World
from pants import Solver

这篇关于如何避免我的代码的用户键入冗余导入行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆