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?

2
  • 1
    Problem is: what if var1 depends on var3, for instance var1 = var3+'foo'. Commented Mar 1, 2017 at 11:14
  • See the top answer here. It's not exactly what you are looking for, but might be a good pointer. stackoverflow.com/questions/33554036/… Commented Mar 1, 2017 at 11:22

4 Answers 4

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']

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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
It highlights variables that haven't been defined
2

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.

Comments

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 .

Comments

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.

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.