Home > Enterprise >  Basic python error with importing module - getting modulenotfound error
Basic python error with importing module - getting modulenotfound error

Time:01-10

I'm doing something basic with python, and I'm getting a pretty common error, but not able to find exactly what's wrong. I'm trying to use a custom module (built by someone else). I have the folder structure like this:

enter image description here

There is the test folder, and I have a file testing.py within that:

enter image description here

The contents of testing.py is:

from util import get_data, plot_data
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

When I run this file, using python testing.py, I get this:

enter image description here

I went through the other questions that speak about paths, and this looks fine, so not sure what I am missing here. My environment is setup using conda, and the environment is active.

CodePudding user response:

Check this answer!!

Use from .util instead of from util

CodePudding user response:

There are a number of ways to specify how to find modules:

  1. Use a relative import:
from .util import get_data, plot_data
  1. Set the environment variable PYTHONPATH includes the directory where you module resides.

  2. See sys.meta_path

CodePudding user response:

Seems like the script was meant to be run from a different directory. People don't usually name a top level module utils. You could try finding a subdirectory of the custom module with a utils dir and running the script from there.

On another note, if this is all the content the script has, it is probably of no real significance and can be casually ignored. The import is never used in the code. The code so far seems to basically do nothing.

CodePudding user response:

Just place utils in the same folder as your testing.py and the python interpreter you put that directory in your path. Other solutions would be to place utils in a directory that is already in your path, since if thats not the case, you cant import from "above" the current directory

  • Related