473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need help with execfile()

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
5 2211
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
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
I just want to access the variables. I do not need to run it.
Dec 9 '10 #5
dwblas
626 Expert 512MB
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

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

Similar topics

7
by: i_vincent | last post by:
Hi all, Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a...
1
by: Bo Jacobsen | last post by:
I have a number of files compiled to bytecode using py_compile.compile(). The .pyc files can be invoked by python directly ($python file.pyc), but "loading" them by execfile just throws an...
0
by: Adam Haskell | last post by:
Ok heres the situation: We have a linked server from SQL 2000 to a foxpro dbf. In our test enviroment we had the Foxpro and SQL server on the same machine. The linked worked perfect. Now we are...
1
by: Srinivasa Ra via .NET 247 | last post by:
(Type your message here) I am writing an application that does lot of read/write's withcomputer's serial port. The application as a whole is workingfine. Current Approach: I have a Timer that...
25
by: Mark | last post by:
I'm just starting out in an introductory ASP.Net course, and am trying to run a simple program but keeping getting an error. I'm running XP, have installed Internet Information Services (5.1) ,...
1
by: Arvind P Rangan | last post by:
Hi, I have created a class library which i need to use in my ASPX file. when i say: Dim mylib As TestLib = new TestLib() it gives me an error saying type required. How do we use a class...
0
by: saravanan_article | last post by:
Hi I am newbie to C#, i am using C# 2005 and DataGridView in my Application. The problem is described here I am using DataGrid and I placed some Headers like Column1,Column2,Column3.... What i...
1
by: Eric_Dexter | last post by:
I was trying to find out what my value for filename is but it will not print. The file I am calling csoundgrid2.main seems to work fine when I call it directly from spe with two values and I have...
8
by: rdabane | last post by:
I'm trying to perform following type of operation from inside a python script. 1. Open an application shell (basically a tcl ) 2. Run some commands on that shell and get outputs from each command...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.