user2828360
user2828360

Reputation: 87

Importing same name file from different directory in python?

I have a directory structure like -

Filetoimport.py code-

call_function()
    do_something
    return

Run.py has -

import filetoimport
filetoimport.call_function()

the dir2 is essentially a copy of dir1 with some changes but run.py is still calling filetoimport from dir1. I don't get what am I missing here?

EDIT 1- dir1 and dir2 are not packages but just plain directories.

Upvotes: 2

Views: 2206

Answers (1)

Lior Cohen
Lior Cohen

Reputation: 5735

I suppose you are running your code from dir1 as the working directory (you can check with os.get_cwd()). Change your working directory to dir2 and it should import the filetoimport.py under dir2.

If you want to be sure what was imported you can print(fileimport.__file__)

Upvotes: 2

Related Questions