使用户定义的包在Windows Python 2.7中工作

时间:2012-12-16 08:01:29

标签: python windows packages

我已经阅读了包上的参考页面,但我似乎无法从我的库文件夹中导入模块。这是我的图书馆:

K:\web\cgi\lib>dir
 Volume in drive K is Krusty
 Volume Serial Number is E889-7AE2

 Directory of K:\web\cgi\lib

16/12/2012  07:52    <DIR>          .
16/12/2012  07:52    <DIR>          ..
16/12/2012  07:52    <DIR>          database
16/12/2012  07:21    <DIR>          kmz_builder
16/12/2012  05:45                21 site_consts.py
16/12/2012  07:42                 0 __init__.py
           2 File(s)             21 bytes
           4 Dir(s)  267,263,594,496 bytes free

这是数据库文件夹:

 K:\web\cgi\lib>dir database
 Volume in drive K is Krusty
 Volume Serial Number is E889-7AE2

 Directory of K:\web\cgi\lib\database

16/12/2012  07:52    <DIR>          .
16/12/2012  07:52    <DIR>          ..
13/12/2012  01:52             3,643 dbops.py
12/12/2012  12:49            11,414 dbspec.py
08/12/2012  11:30             4,104 MyDB.py
               3 File(s)         19,161 bytes
               2 Dir(s)  267,263,594,496 bytes free

设置环境变量:

K:\web\cgi\lib>echo %PYTHONPATH%
k:\web\cgi\lib

我似乎能够从库的顶级(site_consts.py)导入,但不能从子文件夹导入:

K:\web\cgi\lib>python
ActivePython 2.7.1.4 (ActiveState Software Inc.) based on
Python 2.7.1 (r271:86832, Feb  7 2011, 11:33:02) [MSC v.1500 64 bit (AMD64)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> import site_consts
>>> import database.dbspec
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named database.dbspec
>>>

我错过了什么?

1 个答案:

答案 0 :(得分:0)

尝试在__init__.py目录中创建名为database的空白文件。 __init__.py文件告诉Python该目录应被视为package