[英]Cannot import Python module after naming my own file the same name as the module
[英]cannot load my own module after installation
我是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
運行時顯然已經導入了__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
UPDATE
我的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"
]
}
)
問題在於無法從abaqus
找到fem
。 通常,您可以通過使用完整路徑來解決此問題:
from hybrida.fem.conditions import Diriclet
另外,您可以使用包相對導入:
from ....conditions import Diriclet
但是一旦您開始在其中添加太多點,就變成了夢ent般的解開夢really,真的不再值得為此煩惱了。
順便說一句(這完全是我對此事的看法),我發現相對於包的導入很不錯,只要您不需要上樹即可:
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.