为什么我不能导入我自己创建的 Python 包?

Why can't I import my self-created Python package?

我刚刚创建了一个 Python 程序包 (sources here),但安装后我无法使用它。一个简单的导入给了我一个 ImportError.

下面我将准确展示我所做的,以便您可以重现它:

$ git clone git@github.com:kramer65/peewee-versioned.git
Cloning into 'peewee-versioned' 
etc. etc.
Checking connectivity... done.
$ virtualenv venv
New python executable in the/path/to/my/venv/bin/python
Installing setuptools, pip, wheel...done.
$ . venv/bin/activate
(venv) $ cd peewee-versioned/
(venv) $ python setup.py install
running install
etc. etc. everything installs without errors
Finished processing dependencies for peewee-versioned==0.1
(venv) $ cd ..
(venv) $ pip freeze
peewee==2.8.0
peewee-versioned==0.1  # AS YOU CAN SEE IT IS INSTALLED
six==1.10.0
(venv) $ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import peewee
>>> import peewee_versioned
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named peewee_versioned

我的 setup.py 看起来像这样:

from setuptools import setup, find_packages

setup(
    name='peewee-versioned',
    version='0.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'peewee',
        'six'
    ],
)

项目中唯一相关的文件是versioned.py (see the full sources here)

有人知道我做错了什么吗?欢迎所有提示!

[编辑]

我在venv site-packages里查了下有没有我的包相关的egg,有(查看第5个包):

$ ls -l venv/lib/python2.7/site-packages/
total 512
-rw-r--r--   1 kramer65  staff     276 13 apr 13:53 easy-install.pth
-rw-r--r--   1 kramer65  staff     126 13 apr 13:53 easy_install.py
-rw-r--r--   1 kramer65  staff     367 13 apr 13:53 easy_install.pyc
-rw-r--r--   1 kramer65  staff  242923 13 apr 13:53 peewee-2.8.0-py2.7.egg
-rw-r--r--   1 kramer65  staff     970 13 apr 13:53 peewee_versioned-0.1-py2.7.egg
drwxr-xr-x  34 kramer65  staff    1156 13 apr 13:53 pip
drwxr-xr-x  10 kramer65  staff     340 13 apr 13:53 pip-8.1.1.dist-info
drwxr-xr-x   6 kramer65  staff     204 13 apr 13:53 pkg_resources
drwxr-xr-x  52 kramer65  staff    1768 13 apr 13:53 setuptools
drwxr-xr-x  12 kramer65  staff     408 13 apr 13:53 setuptools-20.7.0.dist-info
drwxr-xr-x   5 kramer65  staff     170 13 apr 13:53 six-1.10.0-py2.7.egg
drwxr-xr-x  32 kramer65  staff    1088 13 apr 13:53 wheel
drwxr-xr-x  11 kramer65  staff     374 13 apr 13:53 wheel-0.29.0.dist-info

所以为什么我不能导入它?

由于您在存储库中没有任何包并且只有一个名为 versioned.py 的文件,您应该能够在 运行 之后将其导入为 import versioned python setup.py install。适合我:

$ mkvirtualenv peewee
New python executable in peewee/bin/python
...
$ git clone https://github.com/kramer65/peewee-versioned
Cloning into 'peewee-versioned'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 20 (delta 5), reused 18 (delta 5), pack-reused 0
Unpacking objects: 100% (20/20), done.
Checking connectivity... done.
$ cd peewee-versioned/
$ python setup.py install
running install
...
Finished processing dependencies for peewee-versioned==0.1
$ pip freeze
peewee==2.8.0
peewee-versioned==0.1
six==1.10.0
wheel==0.24.0
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import versioned
>>>

无论如何,既然你想从 peewee_versioned 导入它,我会在这里做两件事:

  • versioned.py 重命名为 peewee_versioned.py
  • 遵循 Listing individual modules 文档段落,使用 py_modules:

    py_modules = ['peewee_versioned']
    

如果您只想要名称,可以使用“import as”功能:

import versioned as peewee_versioned

我认为您必须单独包含版本化模块,因为您没有包。我不认为 find_package 方法会找到单独的模块。

尝试添加 py_modules = ['versioned'] 设置如下:

setup(
    name='peewee-versioned',
    version='0.1.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'peewee',
        'six'
    ],
    py_modules = ['versioned']
)

在此之后,您应该可以导入版本。

执行此操作的正确方法是使您的目录结构如下:

peewee-versioned/
  peewee_versioned/
    __init__.py
    peewee_versioned.py
  setup.py

其中 __init__.py 包含行 import peewee_versioned(或 from peewee_versioned import *),具体取决于您的用例。

通过正确构建文件结构,您可以保护自己免受将来可能有多个文件的更改的影响。