11

I have a long python script which I would like to partly export to smaller scripts. Is there a way to gather all variables which are used in the specific part of the script? I don't want to define all the variables that are not used for the script. Example:

var1 = "folder"
var2 = "something else"
var3 = "another thing"

#part of the script I would like to use seperatly
dosomething(var1,var2)
#end of the part

dosomethingelse(var3)

Is there a way to list var1 and var2 as a part of my script, but not var3 as I won't need it for my new subset script?

Alex
  • 221
  • 1
  • 3
  • 9
  • 1
    Problem is: what if `var1` depends on `var3`, for instance `var1 = var3+'foo'`. – Willem Van Onsem Mar 01 '17 at 11:14
  • See the top answer here. It's not exactly what you are looking for, but might be a good pointer. http://stackoverflow.com/questions/33554036/how-to-get-all-variable-and-method-names-used-in-script – Nir Levy Mar 01 '17 at 11:22

4 Answers4

7

You can use the locals() function to determine what variables have been declared in the namespace.. ie

a = 1
b = 2
print(locals().keys())
c = 3
d = 4
print(locals().keys())

Output

['a', 'b', 'builtins', 'file', 'package', 'name', 'doc']

['a', 'c', 'b', 'd', 'builtins', 'file', 'package', 'name', 'doc']

user2682863
  • 3,097
  • 1
  • 24
  • 38
  • just remember that this is a runtime solution. It means that if there is a variable that only gets defined inside, for example, an ``if`` block that did not get run in this specific run you would not get the name of this variable printed. – Nir Levy Mar 02 '17 at 06:47
  • My example script is quite simple. So locals() might work out. However, if there are several variables before the part of the script it is getting more difficult. And Nir Levy ha a good point, too. – Alex Mar 02 '17 at 07:41
  • agreed, its a 70% solution. For a comprehensive solution you can look at `ast` as others have mentioned. Personally I would just copy the code to a new module and let PyCharm tell me which variables I need – user2682863 Mar 02 '17 at 13:51
  • It highlights variables that haven't been defined – user2682863 Mar 07 '17 at 13:26
1

Simply use dir() or better print(dir()) wherever you want to check the variables. If you write this in global space, it'll return list of global variable and if you write this inside a function, it'll return variables inside that function.

theashwanisingla
  • 412
  • 4
  • 13
1

If you need the variable and the value assigned to it then

import data

for name ,values in vars(data).items():
    print(name, values)

You can choose to store name (all the variable names in the script) or the value attached to it .

suraj
  • 81
  • 4
0

In six steps you can obtain and restore your global variables:

write.py:

NI=str(globals().keys())
NI=eval(NI[NI.find('['):NI.find(']')+1])    #Get the initial situation

S='Test'
S2=input()
D={'a':1,'b':2}
N=1                                         #Create some variables

I=0
I=str(globals().keys())
I=eval(I[I.find('['):I.find(']')+1])
for i in NI: I.remove(i)                    #Get your variables

F=open('datafile.txt','w')
for i in I: F.write(i+'='+repr(eval(i))+'\n')
F.close()                                   #Write on a file

read.py:

F=open('datafile.txt')
F.seek(0)
for i in F.read().splitlines(): exec(i)
F.close()                                   #Read from a file

for i in I: print(repr(eval(i)))            #Read your variables

Replace "globals" with "locals" in "write.py" to get only the script variables.