Home > Back-end >  Relative path call not functioning
Relative path call not functioning

Time:11-11

I'm writing a script for a class and am trying to call a function in another folder into the script, I've done this exactly the way I have it written for many other scripts, though it was in a different respository/workspace. Here's the code:

import sys
sys.path.insert(0, 'Mathematical Functions')
print(sys.path)
import matplotlib.pyplot as plt
import numpy as np
import math as mt
from FourthOrRungeKut import RungeKutta as RK4

The import call of the FourthOrRungeKut script returns error "Import "FourthOrRungeKut" could not be resolved.

For reference, here's an image of my directory:

enter image description here

The interpreter is treating the correct file as the root, it appears the line sys.path.insert(0, 'Mathematical Functions') Is not appending the folder so the interpreter can locate the FourthOrRungeKut script.

For reference, here is the repository path (and the path that os.getcwd() returns):

C:\Users\falco\OneDrive\Documents\GitHub\Num-Methods-Final-Project.

This exact setup works perfectly in another repository I have made.

CodePudding user response:

The problem here is about pathing. FourthOrRungeKut.py is in a folder named Mathematical Functions. When you do from FourthOrRungeKut import RungeKutta as RK4 it looks for FourthOrRungeKut.py in the root of your project.

To fix the problem, you first need to rename Mathematical Functions so that it is a valid module name in python. This means that you cannot use spaces in the folder. If you rename the folder to math, then you can do

from math.FourthOrRungeKut import RungeKutta as RK4

In general, treat file and folder names the as if they are variable names: don't use spaces or special characters.

CodePudding user response:

Add FourthOrRungeKut to extra path then it won't throw the error:

"python.analysis.extraPaths": [
    "./Mathmetical Functions"
]

enter image description here

  • Related