开发者

what are the rules for imports in python

开发者 https://www.devze.com 2023-02-14 15:03 出处:网络
I have a directory structure : ../POC/mud/ client/ common/ server/ and i am trying to use the following 开发者_C百科imports :

I have a directory structure :

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

and i am trying to use the following 开发者_C百科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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号