0

I am trying to reference a variable from one python file to another.

I have a base file that has a variable value, current_time and I want to call this variable in another python function that is in another python file.

This is what I tried doing:

From the file I am trying to call the variable:

ref_path = os.path.split(os.getcwd())[0]
ref_path = os.path.join(ref_path, "folder_1", "folder_2", "folder_3")

Now that I am pointing to the destination folder when I try doing

from ref_path import function as function
 

I am getting an error ModuleNotFoundError: No module named 'ref_path'

I am trying to call function.current_time

1
  • is another file is in different folder and not a part of your project? if yes, i suggest you to make that file a part of your project Commented Jun 15, 2021 at 6:19

2 Answers 2

1

Add the path to base file in your syspath and import the variable.

import sys
sys.path.append(ref_path)

from function import *
# Now you can use the variable
Sign up to request clarification or add additional context in comments.

Comments

0

Could you import the file?

assuming the ref path is the directory next to this file you could try this:

import os

fullPath = os.path.realpath(__file__)  #this file
thisDir = os.path.dirname(fullPath) # this files directory

from ref_file import ref_module 

print(ref_module.GetCurrentTime())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.