20
votes

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.

1
Have you installed the package into the environment? - Busturdust
Thanks for the reply. I didn't get it. Do we need to install it separately. In my windows env it works fine. - Ankur Bhatia
I believe that it is probably a matter of PYTHONPATH issue which is properly configured on windows and not on linux, add the code base to the path, or install it so that it automaticaly locates itself tot he path - Busturdust
Thanks a lot. That was the issue. But how do we configure it to install it automatically. I want users to download the code and run it and not configure the PYTHONPATH. - Ankur Bhatia
create a setup.py users will be able to install the package using python setup.py install Look into writing a proper setup - Busturdust

1 Answers

20
votes

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