我刚刚创建了一个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(请参阅full sources here)
有人知道我在这里做错了什么吗?欢迎所有提示!
[编辑]
我检查了venv网站包中是否有与我的包相关的鸡蛋,并且(检查第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
为什么哦为什么我不能导入它?
答案 0 :(得分:5)
由于您在存储库中没有任何软件包,并且只有一个名为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']
答案 1 :(得分:2)
我认为您必须单独包含版本化模块,因为您没有包。我不认为find_package方法会找到独奏模块。
尝试添加py_modules = [&#39;版本化&#39;]设置如下:
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']
)
在此之后,您应该可以导入版本。
答案 2 :(得分:1)
如果您只想要名称,可以使用“{{3}}”功能:
import versioned as peewee_versioned
答案 3 :(得分:0)
执行此操作的正确方法是使您的目录结构如下:
peewee-versioned/
peewee_versioned/
__init__.py
peewee_versioned.py
setup.py
其中__init__.py
的行import peewee_versioned
(或from peewee_versioned import *
)取决于您的使用情况。
通过正确构建文件,您可以保护自己免受将来可能有多个文件的更改。