简体   繁体   English

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

[英]cannot load my own module after installation

I am new to Python and I'm having a lot of frustration trying to understand an issue with a module I've been working on. 我是Python的新手,我很沮丧地试图理解我一直在研究的模块问题。 The entire development so far was done by running a __main__.py file. 到目前为止,整个开发都是通过运行__main__.py文件完成的。 I've tested the code with different examples, but always inside the __main__.py . 我已经用不同的示例测试了代码,但始终在__main__.py内部。 Now that I want to test the module from the outside , I install it (everything goes well). 现在,我想从外部测试模块,然后安装它(一切顺利)。 But then when I try to load the module I installed it gives me import errors that didn't show up when I was running from __main__.py . 但是,当我尝试加载我安装的模块时,它给了我从__main__.py运行时未显示的导入错误。 The error is the following: 错误如下:

$ 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'

So I don't understand what's happening, because clearly the module is being imported when I run from __main__.py . 所以我不知道发生了什么,因为从__main__.py运行时显然已经导入了__main__.py The only thing I can think of is that there is some circular dependency, that when I try to load the module from the outside, fem is on the top and while is being loaded it can't be seen yet so I can't import it. 我唯一能想到的是存在一定的循环依赖关系,当我尝试从外部加载模块时, fem在顶部,而在加载时还看不到它,因此无法导入它。 But if this is the case, how in the world I solve this problem? 但是,如果是这种情况,那么我该如何解决这个问题呢?

The structure of the project is as follows: 该项目的结构如下:

$ 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

UPDATE UPDATE

My config.py file: 我的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"
        ]
    }

)

The problem is that from abaqus , fem isn't able to be found. 问题在于无法从abaqus找到fem Typically you solve this by using a full path: 通常,您可以通过使用完整路径来解决此问题:

from hybrida.fem.conditions import Diriclet

Alternatively, you could use a package relative import: 另外,您可以使用包相对导入:

from ....conditions import Diriclet

but once you start getting too many dots in there it becomes a nightmare to disentangle and really isn't worth the trouble anymore. 但是一旦您开始在其中添加太多点,就变成了梦ent般的解开梦really,真的不再值得为此烦恼了。

As an aside (this is completely my opinion on the matter), I find that package relative imports are nice as long as you don't need to go up the tree: 顺便说一句(这完全是我对此事的看法),我发现相对于包的导入很不错,只要您不需要上树即可:

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

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

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