0

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.

  • Write a function which returns that variable and you can use it in other – Utpal Dutt Feb 28 '21 at 06:44
  • Yes, you could, but it's a bad idea. it has been answered there. https://stackoverflow.com/questions/17255737/importing-variables-from-another-file – Hui Zheng Feb 28 '21 at 06:42

2 Answers2

0

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.

Dhruv Jadhav
  • 260
  • 1
  • 11
0

Yeah, there is no problem with that:

from file_name import function_name
print(function_name.variable_name)
Xyndra
  • 110
  • 1
  • 11