2

我正在尝试import ZipCodeDatabasehelloworld.py.

  • helloworld.py存在于/google-app-engine/helloworld
  • ZipCodeDatabase模块存在/usr/local/lib/python/python2.7/dist-packages
  • PYTHONPATH=/usr/local/lib/python/python2.7/dist-packages;/usr/local/lib/python/

编译helloworld时我仍然收到“ZipCodeDatabase找不到模块”。为什么不是从 中挑选出来的PYTHONPATH

4

1 回答 1

3

我非常怀疑你有一个名为ZipCodeDatabase. 该命名约定通常保留给class位于module. 模块通常是小写或lower_snake_case,以表示包含模块的文件。我假设你已经安装pyzipcode在这里,但它可能是一个不同的模块。

# assuming pyzipcode.py in the dist-packages directory
$ python -c 'from pyzipcode import ZipCodeDatabase'

如果我在上面错了,那么您确定您正在运行安装了 ZipCodeDatabase 模块的 python 版本吗?

一些故障排除步骤:

$ which python
$ python --version
$ python -c 'import ZipCodeDatabase'
$ ls -l /usr/local/lib/python2.7/dist-packages/ | grep -i zip

另外,你真的有必要指定PYTHONPATH线路吗?通常,该site-packages文件夹(以及我假设dist-packagesUbuntu 上的文件夹)包含在 defaultPYTHONPATH中,以及您正在使用的 python 模块的当前目录。

您是如何安装 ZipCodeDatabase 的?你把文件放在那里了吗?试着把它放在你的helloworld.py文件旁边,然后尝试导入它。此外,完整的堆栈跟踪在这里是有用的信息,尤其是当其他人试图诊断您遇到的问题时。

编辑:

Ok, now that I know you're using google app engine (should have been obvious from your use of paths - I'm sorry), it looks like it doesn't use the site-packages or dist-packages to load modules. You should create a sub-directory in your project with the relevant third party libraries, and add that sub-directory to your path. Disclaimer: I've never used GAE so I might be missing the mark with this.

Check out this answer for how to structure your project and add the extra directory to your path from within the application.

于 2013-04-20T07:36:30.103 回答