0

I am new to writing python packages and trying to figure out how imports inside the package work in Python2 vs Python3.

The structure of my package is:

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!")

I have created a GitHub repository for my package at: https://github.com/jirikadlec2/first-python-package

Next, I tried to install and run my package in Python2 and Python3 virtual environment. My operating system is Ubuntu Linux 16.04.

Installation in Python2 virtual environment (successful):

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

Running the package in Python2 virtual environment (successful):

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!

Installing my package in Python3 virtual environment (successful):

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

Running my package in Python3 virtual environment fails due to import error:

(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'

I don't understand the reason of the ImportError. module_b seems to be correctly installed in the package and the statement: from FirstPythonPackage import module_b doesn't give me any error. What am I doing wrong and what is the correct way to make the imports inside my package work both in Python2 and Python3?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jirikadlec2
  • 1,256
  • 1
  • 23
  • 36
  • 1
    Please remember there's very little reason to support Python 2 in new code anymore. https://pythonclock.org/ – AKX Sep 11 '19 at 07:24

1 Answers1

2

You're likely bumping into absolute imports, and enabling from __future__ import absolute_imports in your Py2 code would also break it.

The compatible way is to simply

from FirstPythonPackage import module_b

or (e.g.)

import FirstPythonPackage.module_b as mb

or

import .module_b as mb

even within your package in both Python 2 and 3.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Indeed, when I added `from __future__ import absolute_import` to the beginning of `module_a`, then I get the same `ImportError: No module named module_b` in both Python 2 and 3. – jirikadlec2 Sep 11 '19 at 07:54