ImportError: No module named <something>

Question:

The following is my directory structure.

ankur
├── ankur1
│   ├── __init__.py
│   └── util.py
├── ankur2
│   └── main.py
└── __init__.py

In main.py, I am importing the following.

import ankur.ankur1.util

When I execute the code in Windows, it works perfectly fine. But in Linux, I get the following error.

ImportError: No module named ankur.ankur1.util

I also read the official python doc on Modules and Packages.

Asked By: Ankur Bhatia

||

Answers:

Your package structure is OK. Your import statement is OK. The only thing missing is for the package to be visible in sys.path, a list of locations where import statements can be resolved.

Usually we do this by "installing" the package locally with pip, which copies your code into site-packages. This directory is one of the entries in sys.path, so when your code is installed in site-packages, the import statements can now be resolved as usual.

However, to install your code you’ll need an installer (setup.py script) or a build system (pyproject.toml file) defined for the package. Your project doesn’t appear to have any installer or build system, so you’ll need to create one (see the Python Packaging User Guide for details about that) and then install the package with pip. If you don’t want to learn Python packaging just yet, you’ll need to find another way around.

It is possible to modify sys.path directly in main.py, which is subsequently enabling the statement import ankur.ankur1.util to be resolved. This is hacky and I recommend against that. It would add the restriction that executing main.py is the only entry point to the rest of the package, and so any other code wanting to import ankur will first need to know the path to main.py on the filesystem. That’s a messy approach and should be avoided.

Another way around is to use the environment – there is an environment variable PYTHONPATH which can be used to augment the default search path for module files. In your shell:

export PYTHONPATH=/path/to/parent  # linux/macOS
SET PYTHONPATH=C:/path/to/parent   # Windows

Where parent is the directory containing ankur subdirectory.

The exact location of site-packages depends on your OS/platform, but you can check with import sysconfig; sysconfig.get_paths()["purelib"]

Answered By: wim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.