python:不同包下同名的两个模块和类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15720593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:46:23  来源:igfitidea点击:

python: Two modules and classes with the same name under different packages

pythonpython-3.xpackagepython-import

提问by Mayank

I have started to learn python and writing a practice app. The directory structure looks like

我已经开始学习python并编写一个练习应用程序。目录结构看起来像

src
 |
 --ShutterDeck
    |
    --Helper
       |
       --User.py -> class User
    --Controller
       |
       --User.py -> class User

The srcdirectory is in PYTHONPATH. In a different file, lets say main.py, I want to access both Userclasses. How can I do it.

src目录是PYTHONPATH。在另一个文件中,可以说main.py,我想访问这两个User类。我该怎么做。

I tried using the following but it fails:

我尝试使用以下方法但失败了:

import cherrypy
from ShutterDeck.Controller import User
from ShutterDeck.Helper import User

class Root:
  @cherrypy.expose
  def index(self):
    return 'Hello World'

u1=User.User()
u2=User.User()

That's certainly ambiguous. The other (c++ way of doing it) way that I can think of is

这当然是模棱两可的。我能想到的另一种(c++ 方式)方式是

import cherrypy
from ShutterDeck import Controller
from ShutterDeck import Helper

class Root:

  @cherrypy.expose
  def index(self):
    return 'Hello World'

u1=Controller.User.User()
u2=Helper.User.User()

But when above script is run, it gives the following error

但是当上面的脚本运行时,它给出了以下错误

u1=Controller.User.User()
AttributeError: 'module' object has no attribute 'User'

I'm not able to figure out why is it erroring out? The directories ShutterDeck, Helperand Controllerhave __init__.pyin them.

我无法弄清楚为什么会出错?该目录ShutterDeckHelperController具有__init__.py在其中。

采纳答案by Martijn Pieters

You want to import the Usermodules in the package __init__.pyfiles to make them available as attributes.

您希望导入User__init__.py文件中的模块以使其可用作属性。

So in both Helper/__init_.pyand Controller/__init__.pyadd:

因此,在这两个Helper/__init_.pyController/__init__.py补充:

from . import User

This makes the module an attribute of the package and you can now refer to it as such.

这使模块成为包的一个属性,您现在可以这样引用它。

Alternatively, you'd have to import the modules themselves in full:

或者,您必须完全导入模块本身:

import ShutterDeck.Controller.User
import ShutterDeck.Helper.User

u1=ShutterDeck.Controller.User.User()
u2=ShutterDeck.Helper.User.User()

so refer to them with their full names.

所以用他们的全名来指代他们。

Another option is to rename the imported name with as:

另一种选择是使用以下命令重命名导入的名称as

from ShutterDeck.Controller import User as ControllerUser
from ShutterDeck.Helper import User as HelperUser

u1 = ControllerUser.User()
u2 = HelperUser.User()

回答by Andrew Gorcester

One way is just:

一种方法是:

import ShutterDeck.Controller.User
import ShutterDeck.Helper.User

cuser = ShutterDeck.Controller.User.User()
huser = ShutterDeck.Helper.User.User()

You can also do this:

你也可以这样做:

from ShutterDeck.Controller.User import User as ControllerUser
from ShutterDeck.Helper.User import User as HelperUser

回答by jaor

This might also help (struggled with similar problem today):

这也可能有帮助(今天遇到了类似的问题):

ShutterDeck
├── Controller
│?? ├── __init__.py
│?? └── User.py
├── Helper
│?? ├── __init__.py
│?? └── User.py
└── __init__.py

in ShutterDeck/{Controller,Helper}/__init__.py:

ShutterDeck/{Controller,Helper}/__init__.py

from .User import User

And then:

进而:

>>> import ShutterDeck.Helper
>>> helperUser = ShutterDeck.Helper.User()
>>> helperUser
<ShutterDeck.Helper.User.User object at 0x1669b90>
>>> import ShutterDeck.Controller
>>> controllerUser = ShutterDeck.Controller.User()
>>> controllerUser
<ShutterDeck.Controller.User.User object at 0x1669c90>