Python包内的导入在python3中不起作用

时间:2019-09-11 07:16:13

标签: python python-3.x python-2.7 python-import python-packaging

我刚开始编写python软件包,并试图弄清该软件包中的导入如何在Python2和Python3中工作。

我的包裹的结构是:

LICENSE
README.md
setup.py
FirstPythonPackage
    __init__.py
    data.json
    module_a.py
    module_b.py

setup.py:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="FirstPythonPackage",
    version="1.0.2",
    author="jirikadlec2",
    author_email="jirikadlec2@example.com",
    description="my first python package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/jirikadlec2/first-python-package",
    packages=setuptools.find_packages(),
    install_requires=["numpy"],
    package_data={"FirstPythonPackage": ["data.json"]},
    classifiers=[
        "Development Status :: 3 - Alpha"
    ],
)

FirstPythonPackage / module_b.py:

import numpy as np

def run_helper(options):
    my_data = np.array([[1, 2],[2, 1]])
    my_result = my_data * 2
    print("module_b, run_helper completed with options: " + options)
    return my_result

FirstPythonPackage / module_a.py:

import module_b

def do_stuff():
    final_result = module_b.run_helper("default_options")
    if final_result:
        print("module_a, do_stuff completed successfully!")
    else:
        print("module_a, error calculating result!")

我在以下位置为我的软件包创建了GitHub存储库:https://github.com/jirikadlec2/first-python-package

接下来,我尝试在Python2和Python3虚拟环境中安装并运行我的软件包。我的操作系统是Ubuntu Linux 16.04。

在Python2虚拟环境中安装(成功):

virtualenv -p python2 env2
source env2/bin/activate
(env2) pip install git+https://github.com/jirikadlec2/first-python-package

在Python2虚拟环境中运行包(成功):

source env2/bin/activate
(env2) python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from FirstPythonPackage import module_a
>>> module_a.do_stuff()
module_b, run_helper completed with options: default_options
module_a, do_stuff completed successfully!

在Python3虚拟环境中安装我的软件包(成功):

virtualenv -p python3 env3
source env3/bin/activate
(env3) pip install git+https://github.com/jirikadlec2/first-python-package

由于导入错误,在Python3虚拟环境中运行我的软件包失败:

(env3) python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FirstPythonPackage import module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
  File "/home/jiri/github/package_test/env3/lib/python3.5/site-packages   /FirstPythonPackage/module_a.py", line 1, in <module>
  import module_b
ImportError: No module named 'module_b'

我不明白ImportError的原因。 module_b似乎已正确安装在软件包中,并且以下语句:from FirstPythonPackage import module_b没有给我任何错误。我在做什么错?使包中的导入在Python2和Python3中都能正常工作的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

您很可能碰上absolute imports, and enabling from __future__ import absolute_imports in your Py2 code would also break it

兼容的方法是简单

from FirstPythonPackage import module_b

或(例如)

import FirstPythonPackage.module_b as mb

import .module_b as mb

甚至在您的软件包中(在Python 2和3中)。