Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

python - ImportError: No module named <something>

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...