question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nesting scripts -- runfile to call a script within a script gives KeyError: '__file__' , but run as a selection works fine

See original GitHub issue

Is there a limitation or a technique needed for nesting scripts in Spyder? Carlos Córdoba asked me to put this into the issues tracker. Here are the nested scripts and symptoms of the problem:

Script1.py is: print ("hello")

Script2.py is: runfile ('Script1.py')

Description of problem: F9 key runs this line of code fine, the text hello prints out without an error

F5 key or another script such as Script3.py: runfile (‘Script2.py’) gives an error message after printing hello:

Output error:

hello
Traceback (most recent call last):

  File "<ipython-input-24-c314cb3c22a8>", line 1, in <module>
    runfile('C:\\Users\\Peter\\Documents\\Roparju Regression\\Accudata\\Base\\Code\\TESTING Step.py')

  File "C:\Users\Peter\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 714, in runfile
    namespace.pop('__file__')

KeyError: '__file__'

Comments:

Partial work-around seems to work sometimes but not others, but did get me around may hang-up: try-except works as a selection run (F9) but not as a module run (F5)

    try:
        runfile('C:\\Users\\Peter\\Documents\\Roparju Regression\\Accudata\\Base\\Code\\TEST_functions.py')
    except:
        print(" ")

I recognize that the pythonic way to call modules is using main and init, but it seems to me runfile should be a viable alternative to that within spyder.

My application has 4 modules called in sequence by a single driver. So, I run the driver to run the 4 modules when I want. But, I also have the option to run (and re-run) the individual modules for testing purposes. Importantly, to run independently, all 4 modules, in turn, need to run a core services module that defines a general set of functions used throughout the application.

I am interested in the better, more pythonic way to code what’s below as long as I have the flexibility to run all, or just a sub-module stand-along (that is, “file read” and “core functions” should be executable):

runfile("driver.py")
    runfile("file read")
        runfile("core functions")
    runfile("data recoding")
        runfile("core functions")
    runfile("model build")
        runfile("core functions")
    runfile("model scoring")
        runfile("core functions")

Versions:

    python 3.6.1
    ipython 6.2.1
    conda 4.3.31
    spyder 3.2.4

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pzajonccommented, Dec 21, 2017

appreciate that tip! @CAM-Gerlach

0reactions
ccordoba12commented, Jan 30, 2018

I guess not. runfile is not meant to be used outside of Spyder and in source code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

runfile to call a script within a script gives KeyError: '__file__ ...
[spyder] Nesting scripts -- runfile to call a script within a script gives KeyError: '__file__' , but run as a selection works fine....
Read more >
How to fix: KeyError: '__file__' in sitecustomize.py (anaconda3 ...
(Spyder maintainer here) This error is fixed in our latest version (3.3.4). Please update by opening the Anaconda prompt and running there
Read more >
1. Python Basics - Foundations for Analytics with Python [Book]
Running a Python script, first_script.py, in Anaconda Spyder. Alternatively, you can run the script in a Command Prompt (Windows) or Terminal window (macOS),...
Read more >
Notification error for custom script - Checkmk Community
I have a custom notification script which uses POST to create a servicenow record. Script installed, but getting the following: CEE 1.6.0p10
Read more >
MATLAB vs Python: Why and How to Make the Switch
Value shows the current value of the variable. Running Code in Files in Spyder. The last stop in our brief tour of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found