如何导入这个包? python中的相对导入

时间:2013-05-20 11:57:33

标签: python import relative-path relative-import

我正在尝试让this在我的机器上工作(拱形,使用gnome 3)。我认为设置正常(我替换了所有的打印命令)但是当我在终端上运行chemlab时,我收到一个错误,告诉我没有核心包:

[yotam@Standing chemlab-master]$ chemlab
Traceback (most recent call last):
  File "/usr/bin/chemlab", line 5, in <module>
    pkg_resources.run_script('chemlab==0.2', 'chemlab')
  File "/usr/lib/python3.3/site-packages/pkg_resources.py", line 505, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python3.3/site-packages/pkg_resources.py", line 1246, in run_script
    exec(compile(open(script_filename).read(), script_filename, 'exec'), namespace, namespace)
  File "/usr/lib/python3.3/site-packages/chemlab-0.2-py3.3-linux-i686.egg/EGG-INFO/scripts/chemlab", line 4, in <module>
    import chemlab as cl
  File "/usr/lib/python3.3/site-packages/chemlab-0.2-py3.3-linux-i686.egg/chemlab/__init__.py", line 1, in <module>
    from core.molecule import Molecule, Atom
ImportError: No module named 'core'
[yotam@Standing chemlab-master]$ ls /usr/lib/python3.3/site-packages/chemlab-0.2-py3.3-linux-i686.egg/chemlab/
__init__.py  contrib/     db/          io/          libs/        resources/   
__pycache__/ core/        graphics/    ipython.py   molsim/      utils/       
[yotam@Standing chemlab-master]$ ls /usr/lib/python3.3/site-packages/chemlab-0.2-py3.3-linux-i686.egg/chemlab/

所以我尝试在本地运行这些文件。我转到相应的文件夹并运行python __init__.py不,我

[yotam@Standing chemlab]$ python __init__.py
Traceback (most recent call last):
  File "__init__.py", line 1, in <module>
    from core.molecule import Molecule, Atom
  File "/home/yotam/Applications/Chemlab/chemlab-master/chemlab/core/__init__.py", line 1, in <module>
    from .molecule import Molecule, Atom
  File "/home/yotam/Applications/Chemlab/chemlab-master/chemlab/core/molecule.py", line 6, in <module>
    from ..libs.ckdtree import cKDTree
ValueError: attempted relative import beyond top-level package
[yotam@Standing chemlab]$ 

我搜索了一下,发现我可以(应该?)用..libs.ckdtree import cKDTree替换chemlab.libs.ckdtree import cKDTree我这样做只是为了得到

  
    
      

追踪(最近一次通话):         文件“”,第1行,in         文件“./core/init.py”,第1行,in           来自.molecule进口Molecule,Atom         文件“./core/molecule.py”,第6行,in           来自chemlab.libs.ckdtree导入cKDTree         文件“/usr/lib/python3.3/site-packages/chemlab-0.2-py3.3-linux-i686.egg/chemlab/init.py”,第1行,in           来自core.molecule import Molecule,Atom       ImportError:无法导入名称Molecule

    
  

哪个让我感到困惑。我该如何导入/运行它?

编辑:文件夹结构:

chemlab
|
|
+  __init__.py
+  core
   |
   |
   +  molecule.py

编辑2: chemlab/__init__.py只有一行     来自core.molecule import Molecule,Atom

和chelab / core / molecule / import部分看起来像

import numpy as np
from collections import Counter
import numpy as np
from copy import copy

from ..libs.ckdtree import cKDTree
from ..db import ChemlabDB
cdb = ChemlabDB()

masses = cdb.get("data", "massdict")

from .attributes import MArrayAttr, MField
from .fields import AtomicField, FieldRequired
from .serialization import data_to_json, json_to_data

1 个答案:

答案 0 :(得分:0)

你有一个循环依赖。但这是由chemlab.__init__.py想要导入core.molecule.Molecule导致的,它不应该尝试做。删除该导入行。