Python 从不同版本导入不同库

Python Import Different Libraries from Different Versions

我正在尝试开发一个 Python 脚本,我似乎 运行 陷入了我想要包含的两个库之间的冲突。

我拥有的第一个依赖项是 pymoos (https://github.com/msis/python-moos),它是连接到我的通信架构所必需的。我已经构建了代码并在以下位置手动安装了结果 pymoos.so:

但是,只有 python2.7 允许我从解释器中 'import pymoos'。从 Python3 版本中的任何一个尝试都会产生这个:

Python 3.6.8 (default, Jun 11 2019, 15:15:01) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymoos
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_pymoos)

第二个依赖项是 pydantic,我只能使用 pip3 安装它,这显然意味着它只能从我安装的 Python3 的任一版本中获得。尝试从 Python2 导入会得到以下结果:

Python 2.7.5 (default, Jun 11 2019, 14:33:56) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydantic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pydantic

因为我知道 pymoos.so 库在哪里,我认为我最简单的方法是将它放在正确的位置以便它与 python3 一起使用,但我似乎做不到找对地方!

根据我的评论:

它应该像 pip install path/to/pymoos/code 一样简单,但您最好使用更广泛使用的数据库库,如 asyncpg 或 psycopg。

关于必须使用 sudo,您可以考虑使用 virtualenv 或类似的方法来为您的应用程序设置专用的 python 环境。