简体   繁体   English

包装后Python子模块不可见

[英]Python submodules not visible after packaging

I've been trying to package my project with setup.py, but I ran into a snag. 我一直在尝试使用setup.py打包我的项目,但我遇到了障碍。

My file structure is as follows: 我的文件结构如下:

root/
  mypackage/
     __init__.py
     mysubmodule1/
        __init__.py
     mysubmodule2/
        __init__.py

I'm using the following configuration in my setup.py, 我在setup.py中使用以下配置,

from setuptools import setup, find_packages
# To use a consistent encoding
from os import path
import glob

here = path.abspath(path.dirname(__file__))

print(find_packages(exclude=['docs', 'tests*']))
setup(
    name='mypackage',
    packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
    install_requires=[...],
    scripts=[...],
)

I build the package with python setup.py install inside a virtualenv, the debug line I printed showed that find_packages located all my pacakges. 我在virtualenv中使用python setup.py install构建了包,我打印的调试行显示find_packages位于我的所有pacakges中。

['mypackage', 'mypackage.submodule1', 'mypackage.submodule2']

When I import my package, I attempted to import mypackage.submodule1.class , but this threw a module not found exception. 当我导入我的包时,我试图导入mypackage.submodule1.class ,但这引发了一个未找到模块的异常。 I checked that all my modules are in the output egg in the virtualenv site-packages, and that I can import my root package. 我检查了所有模块都在virtualenv site-packages的输出egg中,并且我可以导入我的root包。

The output of the dir(mypackage) is as follows: dir(mypackage)的输出如下:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mypackage
>>> dir(mypackage)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__']

Am I missing something? 我错过了什么吗? All the opensource projects I referenced follow this pattern. 我引用的所有开源项目都遵循这种模式。 Thanks 谢谢

Found a work around. 找到了解决方法。 It seems that just running setup.py is not packaging all my files and making it visible. 似乎只运行setup.py并没有打包我的所有文件并使其可见。

So instead I ran my local setup in 2 stages to get all the files on my python environment. 因此,我分两个阶段运行我的本地设置,以获取我的python环境中的所有文件。

This first command will create a distribution with all the files in a dist folder. 第一个命令将创建一个包含dist文件夹中所有文件的分发。

$ python setup.py sdist

$ tree dist
dist
├── TrainingDataAgent-0.0.1-py2.7.egg
└── TrainingDataAgent-0.0.1.tar.gz

$ pip install  dist/TrainingDataAgent-0.0.1.tar.gz  

This allowed my calling program to correctly locate all the files of my python package. 这允许我的调用程序正确定位我的python包的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM