【问题标题】:setup.py installed package can't be importedsetup.py 安装包无法导入
【发布时间】:2016-07-17 06:53:15
【问题描述】:

我使用以下 setup.py 创建了我的第一个包:

from setuptools import setup, find_packages
setup(
    name='mygn',
    version='0.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'lxml==3.3.5',
        'Pillow==3.0.0',
        'requests==2.2.1',
        'xmltodict==0.10.1',
        'pdfrw==0.2',
        'python-dotenv==0.4.0',
        'boto==2.39.0',
        'click==6.4'
    ]
)

我使用 pip install . 将它安装在 virtualenv 中,然后我查看已安装的包

$ pip freeze
boto==2.39.0
click==6.4
mygn==0.1      # <== here it is
lxml==3.3.5
ordereddict==1.1
pdfrw==0.2
Pillow==3.0.0
python-dotenv==0.4.0
requests==2.2.1
xmltodict==0.10.1

然后我尝试导入它,但失败了:

(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 mygn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mygn
>>> import xmltodict  # no problem importing other modules from the pip freeze
>>> 

有什么想法我在这里可能做错了吗?

【问题讨论】:

    标签: python package pip setuptools setup.py


    【解决方案1】:

    Another person trying to import with a similar issue

    这是他们的解决方案:

    chmod -R 775 /usr/local/lib/python2.6/dist-packages/
    

    似乎这个人通过确保他们的访问权限允许他们使用分发包文件夹中的文件解决了他们的问题。我可能会通过确保您的文件及其目录对您开放来做同样的事情来解决问题。不太清楚是什么原因造成的。

    【讨论】:

    • chmod -R 777-ed 整个 virtualenv venv 目录,但不幸的是这没有帮助。还有其他想法吗?
    • 我想知道在 find_packages 排除项中使用单引号是否可以像您使用的那样与通配符一起使用。我只见过人们使用双引号。即使对于大多数示例,例如:packages=find_packages(exclude=[".tests", ".tests.*"])。我也看到的一件事是,即使您有时在 package_dataMANIFEST.in 文件中指定信息 include_data_packages= True 实际上不会使用您尝试包含的所有文件。您可能还想尝试 distutils 而不是 setuptools。
    猜你喜欢
    • 2021-05-20
    • 2013-02-28
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多