Home > front end >  importing a function defined in a different directory
importing a function defined in a different directory

Time:07-05

Disclaimer: I am studying Python. I am given a task of reusing functions.

the function is simple:

app/utils/calculators.py

  def calculate_session(session_type, session_code):
      return 3 # just to save space

Now i need to use the function from a different file but for the life of me, I failed to import it. I have already added init.py to utils directory, to the app directory as well.

app/tasks/process_sessions.py

   from utils.calculators import calculate_session

but when I run it, it fails saying module not found. I am in a virtual environment and all the files go in app directory.

What am I missing?

CodePudding user response:

You'll want to specify the full path of the file (including the root directory) and import the specific function. Example below:

from app.utils.calculators import calculate_session

This should then import your function, regardless of whether you're using a virtual environment.

  • Related