is it possible to import a variable inside a function from one python script to another python script?

Question:

Apparently i have found out a way to import functions from one python script to another.

  from file_name.py import function_name

But is it possible to transfer variables as well.

Apologies cause I’m new to python.

Answers:

Yes you can import variables too. You can import the variables the same way you import functions.

from file_name import variable_name

The .py isn’t necessary. For the above statement to work the file_name has to be in the same directory as the file you are working on. Otherwise instead of file_name you can give the path to the file. i.e C:User...file_name.

Answered By: Dhruv Jadhav

Yeah, there is no problem with that:

from file_name import function_name
print(function_name.variable_name)
Answered By: Xyndra

from file_name import function_name
print(function_name)

Answered By: Sin Wee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.