Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How python deals with module and package having the same name?

Suppose I have a module foo.py and a package foo/. If I call

import foo 

which one will be loaded? How can I specify I want to load the module, or the package?

like image 760
Charles Brunet Avatar asked Jun 18 '11 04:06

Charles Brunet


People also ask

Is module and package is same in Python?

A module is a file containing Python code. A package, however, is like a directory that holds sub-packages and modules.

How do you name packages and modules in Python?

Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Can two different packages have modules with same name?

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 are packages and module related to each other?

A package is a collection of python modules under a common namespace. In practice one is created by placing multiple python modules in a directory with a special __init__.py module (file). A module is a single file of python code that is meant to be imported.


2 Answers

I believe the package will always get loaded. You can't work around this, as far as I know. So change either the package or the module name. Docs: http://docs.python.org/tutorial/modules.html#the-module-search-path

like image 183
zeekay Avatar answered Oct 10 '22 00:10

zeekay


Actually, it is possible, by manually guiding the import machinery to use a .py file instead of directory. (This code is not well tested, but seems to work). UPDATE 2020: Note that this requires using custom import_module() function instead of normal import statement. However, with modern Python3 and its importlib, it might be possible to make the bare import statement to work the same way too. (Note that this answer shows flexibility which Python offers. It's not an encouragement to use this in your applications. Use this only if you know what you're doing.)

File foo.py

print "foo module loaded" 

File foo/__init__.py

print "foo package loaded" 

File test1.py

import foo 

File test2.py

import os, imp  def import_module(dir, name):     """ load a module (not a package) with a given name          from the specified directory      """     for description in imp.get_suffixes():         (suffix, mode, type) = description         if not suffix.startswith('.py'): continue         abs_path = os.path.join(dir, name + suffix)         if not os.path.exists(abs_path): continue         fh = open(abs_path)         return imp.load_module(name, fh, abs_path, (description))  import_module('.', 'foo') 

Running

$ python test1.py  foo package loaded  $ python test2.py  foo module loaded 
like image 28
abb Avatar answered Oct 10 '22 01:10

abb