By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Need help with execfile()

P: 3
I'm trying to use execfile() in a Python program, but I do not know why it works in one situation and not work in another. I'm really new to Python and so I do not have enough experience to figure this out. Plus I'm not a programmer.

In this situation, it does not work:
#file1.py
mynums=[1,2,3]

#file2.py
def mystuff():
<tab>execfile('file1.py')
<tab>print mynums

mystuff()

This is giving me an error "mynums not defined"

In this situation it does work:
#file3.py
execfile('file1.py')
print mynums

>>> [1,2,3]

I'm using Python 2.7. All files are in the same folder.
Dec 1 '10 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 624
execfile() executes a file, so you would use:
Expand|Select|Wrap|Line Numbers
  1. ## file1.py
  2. mynums=[1,2,3]
  3. print mynums
  4.  
  5. #file2.py
  6. def mystuff():
  7.     execfile('file1.py')
  8.  
  9. mystuff()
  10. #
  11. # or
  12. ## file1.py
  13. mynums=[1,2,3]
  14.  
  15. #file2.py
  16. import file1
  17. def mystuff():
  18.     print file1.mynums 
Dec 1 '10 #2

P: 3
In my situation I can not use import because import does not work within the module mystuff() which is why I'm trying to get execfile() to work. It is my understanding that it should work in a function like mystuff().
I have another larger program that I'm trying to get to work by using execfile(). This program is a record and playback type of program using serial communication. I have several folders that contains a setup file for configuring the serial port, etc. Each folder has this file but each can have different settings and the settings in each can be changed often so I have to evaluate this file after I start my main program. I use a Tkinter GUI to first get the file path and then use this path to get the setup file. The setup file is a Python file because it is easier to use tuples and definitions. At this point I'm deep into function calls so I can't go back to the main level (also using threading).

Hope this helps explain my needs. Thanks!
Dec 1 '10 #3

Expert 100+
P: 624
Do you want to run the second file or do you want access to the variables in that file, or both.
Dec 3 '10 #4

P: 3
I just want to access the variables. I do not need to run it.
Dec 9 '10 #5

Expert 100+
P: 624
In this situation, it does not work:
#file1.py
mynums=[1,2,3]

#file2.py
def mystuff():
<tab>execfile('file1.py')
<tab>print mynums

mystuff()

This is giving me an error "mynums not defined"
To reiterate, "mynums" is in the file1 name space, so your code should read:
Expand|Select|Wrap|Line Numbers
  1. #file1.py
  2. mynums=[1,2,3]
  3.  
  4. #file2.py
  5. import file1
  6. def mystuff():
  7.     print file1.mynums
  8.  
  9. mystuff() 
You can also call a function in file1 that returns the variable(s).
Expand|Select|Wrap|Line Numbers
  1. #file1.py
  2. def return_var():
  3.     mynums=[1,2,3]
  4.     return mynums
  5.  
  6. #file2.py
  7. import file1
  8. def mystuff():
  9.     print file1.return_var()     ## also in the file1 name space
  10.  
  11. mystuff() 
Dec 9 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.