安装后无法加载我自己的模块

时间:2015-01-22 17:54:46

标签: python python-3.x python-import

我是Python的新手,我在尝试理解我正在研究的模块的问题时遇到了很多挫折。到目前为止,整个开发都是通过运行__main__.py文件来完成的。我使用不同的示例测试了代码,但总是在__main__.py内部。现在我想从外部测试模块,我安装它(一切顺利)。但是当我尝试加载我安装的模块时,它会给我导入错误,当我从__main__.py运行时,它没有显示出来。错误如下:

$ python3
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hybrida
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/__init__.py", line 3, in <module>
    from . import fem
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/__init__.py", line 4, in <module>
    from . import input
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/input.py", line 5, in <module>
    from .mesh.formats import readers
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/mesh/__init__.py", line 3, in <module>
    from . import mesh
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/mesh/mesh.py", line 14, in <module>
    from . import formats
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/mesh/formats/__init__.py", line 5, in <module>
    from . import abaqus
  File "/Users/aaragon/.virtualenvs/pydev/lib/python3.4/site-packages/hybrida-0.1.0-py3.4.egg/hybrida/fem/mesh/formats/abaqus.py", line 7, in <module>
    from fem.conditions import Dirichlet
ImportError: No module named 'fem'

所以我不明白发生了什么,因为当我从__main__.py运行时,显然正在导入模块。我唯一能想到的是存在一些循环依赖,当我尝试从外部加载模块时,fem位于顶部,而加载时它可以&但是还没有看到,所以我无法导入它。但如果是这样的话,我在世界上如何解决这个问题?

项目结构如下:

$ tree .
.
├── AUTHORS.rst
├── CHANGELOG.rst
├── MANIFEST.in
├── README.rst
├── appveyor.yml
├── bootstrap.py
├── setup.cfg
├── setup.py
└── src
    ├── hybrida
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── fem
    │   │   ├── __init__.py
    │   │   ├── conditions
    │   │   │   ├── __init__.py
    │   │   │   ├── dirichlet.py
    │   │   │   └── neumann.py
    │   │   ├── input.py
    │   │   ├── mesh
    │   │   │   ├── __init__.py
    │   │   │   ├── elements
    │   │   │   │   ├── __init__.py
    │   │   │   │   ├── common.py
    │   │   │   │   ├── element.py
    │   │   │   │   ├── quadrangle.py
    │   │   │   │   ├── quadrature.py
    │   │   │   ├── formats
    │   │   │   │   ├── __init__.py
    │   │   │   │   ├── abaqus.py
    │   │   │   │   ├── gmsh.py
    │   │   │   │   ├── groups.py
    │   │   │   │   └── vtk.py
    │   │   │   ├── material
    │   │   │   │   ├── __init__.py
    │   │   │   │   └── stress.py
    │   │   │   └── mesh.py
    │   │   ├── simulation.py
    │   │   └── step.py
    │   └── utils
    │       ├── __init__.py
    │       └── classes.py
    └── hybrida.egg-info
        ├── PKG-INFO
        ├── SOURCES.txt
        ├── dependency_links.txt
        ├── entry_points.txt
        ├── not-zip-safe
        └── top_level.txt

更新

我的config.py文件:

#!/usr/bin/env python3

# -*- encoding: utf-8 -*-
import glob
import io
import re
from os.path import basename
from os.path import dirname
from os.path import join
from os.path import splitext

from setuptools import find_packages
from setuptools import setup


def read(*names, **kwargs):
    return io.open(
        join(dirname(__file__), *names),
        encoding=kwargs.get("encoding", "utf8")
    ).read()

setup(
    name="hybrida",
    version="0.1.0",
    license="BSD",
    description="Enter a description for hybrida here",
    long_description="%s\n%s" % (read("README.rst"), re.sub(":obj:`~?(.*?)`", r"``\1``", read("CHANGELOG.rst"))),
    author="Alejandro M. Aragon",
    author_email="alejandro.aragon@fulbrightmail.org",
    url="https://github.com/alejandro-aragon/hybrida",
    packages=find_packages("src"),
    package_dir={"": "src"},
    py_modules=[splitext(basename(i))[0] for i in glob.glob("src/*.py")],
    include_package_data=True,
    zip_safe=False,
    classifiers=[
        # complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: BSD License",
        "Operating System :: Unix",
        "Operating System :: POSIX",
        "Operating System :: Microsoft :: Windows",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2.6",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: Implementation :: CPython",
        "Programming Language :: Python :: Implementation :: PyPy",
        "Topic :: Utilities",
    ],
    keywords=[
        # eg: "keyword1", "keyword2", "keyword3",
    ],
    install_requires=[
        # eg: "aspectlib==1.1.1", "six>=1.7",
    ],
    extras_require={
        # eg: 'rst': ["docutils>=0.11"],
    },
    entry_points={
        "console_scripts": [
            "hybrida = hybrida.__main__:main"
        ]
    }

)

1 个答案:

答案 0 :(得分:1)

问题在于abaqusfem无法找到。通常,您可以使用完整路径来解决此问题:

from hybrida.fem.conditions import Diriclet

或者,您可以使用包相对导入:

from ....conditions import Diriclet

但是一旦你开始在那里得到太多的点,它就变成了解开的噩梦,并且不再值得麻烦了。

顺便说一句(这完全是我对此事的看法),我发现只要你不需要上树,包相对进口就很好了:

from .foo import Bar  # Ok
from . import something  # Ok
import .baz  # Yep
from ..foo import something  # Meh, not worth the confusion...