Need help with execfile()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Kumataka
    New Member
    • Dec 2010
    • 3

    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.
    Last edited by Bryan Kumataka; Dec 1 '10, 01:49 AM. Reason: line spacing was wrong
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    execfile() executes a file, so you would use:
    Code:
    ## file1.py
    mynums=[1,2,3]
    print mynums
    
    #file2.py
    def mystuff():
        execfile('file1.py')
    
    mystuff()
    #
    # or
    ## file1.py
    mynums=[1,2,3]
    
    #file2.py
    import file1
    def mystuff():
        print file1.mynums

    Comment

    • Bryan Kumataka
      New Member
      • Dec 2010
      • 3

      #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!

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Do you want to run the second file or do you want access to the variables in that file, or both.

        Comment

        • Bryan Kumataka
          New Member
          • Dec 2010
          • 3

          #5
          I just want to access the variables. I do not need to run it.

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            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:
            Code:
            #file1.py
            mynums=[1,2,3]
            
            #file2.py
            import file1
            def mystuff():
                print file1.mynums
            
            mystuff()
            You can also call a function in file1 that returns the variable(s).
            Code:
            #file1.py
            def return_var():
                mynums=[1,2,3]
                return mynums
            
            #file2.py
            import file1
            def mystuff():
                print file1.return_var()     ## also in the file1 name space
            
            mystuff()

            Comment

            Working...