Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named <something>

Tags:

I know this question has been asked multiple times. I have read through all of them but could not resolve my issue.The following is my directory structure.

ankur
     --ankur1
             __init__.py
             util.py
     --ankur2
             main.py
     --__init__.py

In the 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.

like image 760
Ankur Bhatia Avatar asked Apr 18 '17 15:04

Ankur Bhatia


People also ask

How do I fix the ImportError in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

What is ImportError in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.

Why can't Python find my module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Why do I need __ init __ py?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


1 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"]

like image 154
wim Avatar answered Oct 20 '22 22:10

wim