Importing python file from subdirectory of sys path anaconda git repo

Viewed 62

I have a git repo with the following structure:

myrepo
|---scripts
|    |---foo.py
|    |---bar.py

I am trying to import foo in the bar.py file like so:

import sys
import os.path

mypath = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.append(mypath)

import scripts.foo

However, this is returning a ModuleNotFoundError.

Interestingly, this works if I change it to:

import sys
import os.path

mypath = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.append(mypath+'/scripts')

import foo

What is more puzzling is that a colleague of mine can not reproduce this error. I am using a newer version of python (3.8.3 vs 3.6.9) and I am using Anaconda whereas he is not. But I can't see why either of these things should make a difference.

What is going on?

0 Answers
Related