Home > front end >  Adding Function from another python file
Adding Function from another python file

Time:07-01

I'm a python beginner and I'm trying to read a function from one python file into another. However I am getting stuck with an import error. My directories are like this:

test folder

  • plot.py
  • setup folder containing (mapsetup.py, empty __init __.py file)

in mapsetup.py I have the following code:

def read_files (project_name):   
  extent_dir =(os.path.join (dir['extents_dir'],project_name, 'extent_'    project_name   '.shp') )

and then in plot.py I have this

project_name = str(input())

sys.path.append('/Users/Documents/python/test/setup')
from setup.mapsetup import read_files

but I keep getting this error:

ImportError: cannot import name 'read_files' from 'setup.mapsetup'

CodePudding user response:

Presumably there is a function called read_files (check spelling).

There is a double use of the setup in path. So remove one or the other.

like this:


# either
sys.path.append('/Users/Documents/python/test/setup')
from mapsetup import read_files    # <-- removed

# or this
sys.path.append('/Users/Documents/python/test')   # <-- removed
from setup.mapsetup import read_files

Note, it you are using an editor like vscode, the intellisense will usually pick all this up as you are typing, so you will know that you are in the right place. very helpful.

  • Related