Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

The src directory is in PYTHONPATH. In a different file, lets say main.py, I want to access both User classes. How can I do it.

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

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, Helper and Controller have __init__.py in them.

like image 352
Mayank Avatar asked Mar 30 '13 16:03

Mayank


People also ask

Can we have more than one class with same name in different package in Python?

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.

How can I import two modules with same name?

To import two classes with the same name, use the as keyword to rename one or both of the imports, e.g. import {Employee as Employee2} from './another-file-2. js'; . The as keyword allows us to change the identifying name of the import.

Can two classes have same name in Python?

yes, if you define a class with the same name as an already existing class, it will override the definition. BUT existing instances of the first class will still behave as usual.

What is a module in Python What is a package What is the difference between packages and modules in Python?

A module is a file containing Python code in run time for a user-specific code. A package also modifies the user interpreted code in such a way that it gets easily functioned in the run time. A python “module” consists of a unit namespace, with the locally extracted variables.


3 Answers

You want to import the User modules in the package __init__.py files to make them available as attributes.

So in both Helper/__init_.py and Controller/__init__.py add:

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:

from ShutterDeck.Controller import User as ControllerUser from ShutterDeck.Helper import User as HelperUser  u1 = ControllerUser.User() u2 = HelperUser.User() 
like image 174
Martijn Pieters Avatar answered Sep 22 '22 22:09

Martijn Pieters


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
like image 30
Andrew Gorcester Avatar answered Sep 20 '22 22:09

Andrew Gorcester


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:

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>
like image 21
jaor Avatar answered Sep 20 '22 22:09

jaor