Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the rules for imports in python

I have a directory structure :

../POC/mud/
            client/
            common/
            server/

and i am trying to use the following imports :

from mud.server import config
from mud.common.lib import util

but when i try to import config , i get an error:

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mud.server import config
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named mud.server
>>> from mud.common.lib import util
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named mud.common.lib
>>> 

Do i need to be in a certain location for imports to work, or the modules need to be compiled on the OS ? these "mud" modules are just a collection of python .py files

like image 259
kamal Avatar asked Sep 18 '25 09:09

kamal


1 Answers

First you have to have __init__.py file in the mud and sub-folders, the file can be empty though. Take a look at the Python tutorial in the packages section: http://docs.python.org/tutorial/modules.html#packages

In addtion you need to be in the POC folder for the import statements to work or you set the PYTHONPATH env var accordingly or update sys.path dynamically.

like image 136
Bernhard Avatar answered Sep 20 '25 23:09

Bernhard