3 Most Common Import Problems in Python

On May 17, 2017, in Other Online Technology, by James Liu

I am working on Python language programming these days. It’s my first time to work with Python. From my point of view, it’s so easy to learn and it’s very efficient to write app with Python. Compare with PHP or Java, it saves me a lot of time to write extra code. During the time, the most difficult problem I’ve met is the module management. In this post, I will share the most 3 common import problems in my Python project.

In Python, it’s very easy to define a module. According to my understanding, I think it’s possible to define one module in one Python file. For example, I create a file named hello.py, and copy following code inside:

# coding=utf-8
def sayHello():
    print("hello world")

The hello.py will be a python module, and I can use it by import it in another file or modules. For example, I create a file named main.py in the same folder as hello.py

# coding=utf-8
from hello import sayHello

sayHello()

The above example is the simplest case. Once the project becomes bigger and more complicated, I have to manage the modules by package. So what’s the Python package? In Python, the package concept is a little bit like package in other language, for instance, Java. In Python one folder is a package, but in each folder, I need to put a empty file named __init__.py to claim that folder is a Python package. I hope now you already have a basic image about Python package and module, because later I will show you 3 most common errors when I am working with packages and modules in Python.

#1. ImportError No Module Named

ImportError: No module named module_name

If you want to import module in the same folder, it’s highly because of type error. Therefore, please check if there are some typo first. If you want to import module in sub folder, then please check if the sub folder is claimed as package, which means check if there is __init__.py file in the folder.

#2. SystemError Parent Module not Loaded

SystemError: Parent module ” not loaded, cannot perform relative import

This error is more complicated. You can also check this post on stackoverflow. For the example in stackoverflow, if you want to run a python file in the package, you must run it outside of the package by following command:

python3 -m packageone.packagetwo.python_file_name

To be honest, in that post in stackoverfolw, I don’t agree with the idea to use PYTHONPATH, when the project becomes very huge, it could make the project into mess.

ValueError Attempted Relative Import Toplevel Package

ValueError: Attempted relative import beyond toplevel package

For people who is familiar with Python, I guess it could not be a problem. But for new newbie, this error takes me several hours to figure it out. You can also find this similar problem on stackoverflow. The solution from stackoverflow is moving the main.py outside of the package and run it with python. Actually another solution is the same as problem #2 above.

 

Leave a Reply

Weboy

WordPress Blog