简体   繁体   English

可以从构建目录导入模块,但不能从安装目录导入模块

[英]Module can be imported from build directory but not from installed directory

This is encountered on Ubuntu 16.04.2, with GCC 5.4.0 and Python 3.5.2. 在具有GCC 5.4.0和Python 3.5.2的Ubuntu 16.04.2上会遇到这种情况。 So I built a module and inside the build directory I can easily import it without problem: 因此,我构建了一个模块,并且在build目录中可以轻松导入它,而不会出现问题:

➜  build git:(master) ✗ python3 -c "import foo"
➜  build git:(master) ✗ 

However, when I install the module into install_dir (the module was built with cmake/make and then make install --prefix=$install_dir ) and add its location into PYTHONPATH , it doesn't work: 但是,当我将模块安装到install_dir (该模块是使用cmake/make ,然后make install --prefix=$install_dir )并将其位置添加到PYTHONPATH ,它不起作用:

➜  ~ cd
➜  ~ echo $PYTHONPATH
$install_dir/lib/python3.5/site-packages:/usr/local/lib/python3.5/site-packages:
➜  ~ python3 -c "import foo"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "$install_dir/lib/python3.5/site-packages/foo/__init__.py", line 17, in <module>
    from . import cpp
  File "$install_dir/lib/python3.5/site-packages/foo/cpp/__init__.py", line 43, in <module>
    exec("from . import %s" % module_name)
  File "<string>", line 1, in <module>
  File "$install_dir/lib/python3.5/site-packages/foo/cpp/common.py", line 21, in <module>
    _common = swig_import_helper()
  File "$install_dir/lib/python3.5/site-packages/foo/cpp/common.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
SystemError: initialization of _common raised unreported exception

Note: naming has been changed to make the case more generous. 注意:命名已更改,使案子更加大方。 The funny thing is I ran the same script for 3 different devices with the fresh installed OS, only 1 has this problem (!). 有趣的是,我为3个不同的设备(全新安装的OS)运行了相同的脚本,只有1个出现此问题(!)。

What could be the main cause here that I can investigate further? 我可以在这里进行进一步调查的主要原因可能是什么? Or is this something funny caused by Python 3? 还是这是由Python 3引起的有趣?

Run into this problem today. 今天遇到这个问题。

The solution for me was: 我的解决方案是:

  • upgrade petsc (3.7.3->3.7.6) and slepc (3.7.3->3.7.4) 升级petsc (3.7.3-> 3.7.6)和slepc (3.7.3-> 3.7.4)
  • reinstall petsc4py (3.7.0) and slepc4py (3.7.0) 重新安装petsc4py (3.7.0)和slepc4py (3.7.0)

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

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