Repository: spyder-ide/spyder

Issue:
runfile is not defined ?
## Description of your problem

**What steps will reproduce the problem?**
1. on latest spyder 2016-09-15 type a script on editor and run it (in winpython latest)
2. ipython console says "runfile" doesn't exist ?
3. 

**What is the expected output? What do you see instead?**

runfile is found and there is no issue

**Please provide any additional information below**
![spyder-20160917runfile](https://cloud.githubusercontent.com/assets/4312421/18606347/d917864e-7cad-11e6-85ca-9c8d639a82e9.PNG)
## Versions and main components
- Spyder Version: github 2016-09-16
- Python Version: 3.5
- Operating system: Windows10 (winpython) 
## Dependencies

Please go to the menu entry `Help > Optional Dependencies` (or
`Help > Dependencies`), press the button `Copy to clipboard`
and paste the contents below:
jedi >=0.8.1     :  0.9.0 (OK)
matplotlib >=1.0 :  2.0.0b4 (OK)
nbconvert >=4.0  :  4.2.0 (OK)
numpy >=1.7      :  1.11.1 (OK)
pandas >=0.13.1  :  0.19.0rc1 (OK)
pep8 >=0.6       :  1.7.0 (OK)
pyflakes >=0.6.0 :  1.3.0 (OK)
pygments >=2.0   :  2.1.3 (OK)
pylint >=0.25    :  1.6.4 (OK)
qtconsole >=4.2.0:  4.2.1 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
sphinx >=0.6.6   :  1.4.6 (OK)
sympy >=0.7.3    :  1.0 (OK)



Discussion:
there is no issue if I choose to "run in a dedicated python console" or in an "terminal external windows"

What is your ipython version ?

I'll take a look at this one. It's very strange that `runfile` doesn't appear defined.

@Nodd  Ipython 5.1.0 (all the latest officials wheels from Jupyter/Ipython system..) 

Do you need me to check Beta6 was ok ? (I would think problems are with the big change https://github.com/spyder-ide/spyder/pull/2878 merged 3 days ago)

@stonebig, this means (most probably) that there's an error in `sitecustomize`. Please run in a console:

```
set SPYDER_DEBUG=3
spyder --show-console
```

and check if there are any errors printed in our Python or IPython consoles

this does nothing but showing 1 second the spyder "splash" screen. and no DOS error.  

But there have to be errors printed in our Python consoles, at least, or something saying that sitecustomize failed to load.

Can't you see anything like that?

well, for my case I should type:

```
set SPYDER_DEBUG=3
spyder.exe --show-console
```

yet nothing visible... trying to see further, it seems I have hidden "Console Windows Host" processes in background

Not sure whether this is helpful:

I see the same issue of 'NameError: name 'runfile' is not defined' and also the other issue
At spyder start, Kernel is said "dead and restarting" a lot of times (on a slow pc) #3429.

(py3x) set SPYDER_DEBUG=3
(py3x) C:\Users\wt>spyder.exe --show-console
Traceback (most recent call last):
  File "C:\Anaconda\envs\py3x\Scripts\spyder-script.py", line 2, in <module> start.main()
  File "C:\Anaconda\envs\py3x\lib\site-packages\spyder\app\start.py", line 104, in main mainwindow.main()
  File "C:\Anaconda\envs\py3x\lib\site-packages\spyder\app\mainwindow.py", line 2955, in main or options.optimize)
  File "C:\Anaconda\envs\py3x\lib\site-packages\spyder\utils\windows.py", line 33, in set_attached_console_visible
    return bool(ShowWindow(console_window_handle, flag[state]))
KeyError: 3

Changing the debug level:

(py3x) set SPYDER_DEBUG=0'
(py3x) C:\Users\wt>spyder.exe --show-console

I get many lines of:
QWindowsFontDataBase::createEngine: CreateFontFromLOGFONT failed()

And whenever spyder was maximized in the previous run I also get one line of:
QWindowsWindow::setGeometry: Unable to set geometry 3780x2220+45+102 on QWidgetWindow/'MainWindowClassWindow'. Resulting geometry:  3780x2088+45+102 (frame: 16, 72, 16, 16, custom margin: 0, 0, 0, 0, minimum size: 368x263, maximum size: 16777215x16777215).

spyder --reset doesn't change the subsequent behaviour.

I have windows 10, anaconda, display 3840x2160 (with 250% size scaling)

## Version and main components
- Spyder Version: 3.0.0.dev0 
- Python Version: 3.5.1
- Qt Versions: 5.6.0, PyQt5 5.6 on Windows

## Dependencies

```
jedi >=0.8.1     :  0.9.0 (OK)
matplotlib >=1.0 :  1.5.1 (OK)
nbconvert >=4.0  :  4.1.0 (OK)
numpy >=1.7      :  1.10.4 (OK)
pandas >=0.13.1  :  0.18.0 (OK)
pep8 >=0.6       :  1.7.0 (OK)
pyflakes >=0.6.0 :  1.1.0 (OK)
pygments >=2.0   :  2.1.3 (OK)
pylint >=0.25    :  1.5.4 (OK)
qtconsole >=4.2.0:  4.2.0 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
sphinx >=0.6.6   :  1.3.1 (OK)
sympy >=0.7.3    :  1.0 (OK)

```

Thanks, but the error in sitecustomize should be printed in our Python consoles, not directly in the terminal where Spyder was started. Please try to take a look there :-)

activating the console by hand, I see nothing there, but the death of the kernel:
![spyder-20160917runfile3](https://cloud.githubusercontent.com/assets/4312421/18609785/df592c12-7d0a-11e6-834f-f53027843621.PNG)

I don't know then :-/ I don't understand how `runfile` is not defined. I can't see any error in my Windows tests.

another look, at the sys.path:

```

import sys

sys.path
Out[4]: 
['',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages\\spyder\\utils\\site',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\python35.zip',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\DLLs',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages\\win32',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages\\win32\\lib',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages\\Pythonwin',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\python-3.5.2\\lib\\site-packages\\IPython\\extensions',
 'C:\\WinPython\\basedir35\\buildQt5\\winpython-32bit-3.5.x.3\\settings\\.ipython']
```

there is no possible effect of Matplotlib2.0.0b4 ? (I see in an old changelog "sitecustomize error when using Matplotlib 1.5 with Python 2")

error also there with PyQt4

Maybe, I haven't tested with Matplotlib 2.0.0b4. Could you test with 1.5.3?

will do. also maybe the kernel initial death is generating new kernel with "runfile" lost ?

same error with Matplotlib 1.5.3...
will now try with earlier build of Spyder (3.0.0b6) ... b6 works like a charm

a side-remark, First time I start a just created WinPython, Spyder tries to go to internet. never figured why, as I did put the parameter "check for Update on startup" to "False"

other check: it fails with Spyder-3.0.0b7 official wheel, on Qt5.7.0 Python3.5.2

Hello, 

I upgraded to 3.0.0b7 and this issue started immediately
`NameError: name 'runfile' is not defined`

I'm on Ubuntu 14.04.3

Idem on OSX 10.11.6
The first thing I see in the IPython console is:
`Kernel Died. Restarting.`

I also have:
`NameError: name 'debugfile' is not defined`

I had the same problem after upgrading from 3.0.0b4 to 3.0.0b7. The error seemed to disappear when I ran `sitecustomize.py` manually from within Spyder 3 (from the folder `\Python3.4\Lib\site-packages\spyder\widgets\externalshell`). I suspect some kind of _path_ error. After adding the folder `externalshell` folder to the `PYTHONPATH` in Spyder (through PYTHONPATH manager in the Tools menu), it seems the _runfile is not defined_ error stays away (also after a refresh restart).

I have the same problem with Spyder version 3.0.0b7 installed with conda. I think the problem is that `spyder/utils/site/sitecustomize.py` is not installed. I don't understand how the package get built, but comparing the `spyder/utils/site` subdirectory (which is not installed) with the `spyder/utils/external`, perhaps the problem is a missing `spyder/utils/site/__init__.py` file?

When running Spyder from a git clone using `bootstrap.py`, the error disappears.

I confirm @jitseniesen suggestion works. I copied spyder/utils/introspection/**init**.py to spyder/utils/site/**init**.py and rebuild the wheel, and "runfile" is found again

+1 for this problem when installed with `conda`. I used stonebig's suggestion (git clone, copied `spyder/utils/introspection/__init__.py` to `spyder/utils/site/__init__.py`) and used `python setup.py install` to solve this issue.

Yes, the problem is the `spyder/utils/site` directory didn't get packaged because of the lack of an `__init__.py` file in that dir (as @jitseniesen suggested ;-).

I'll solve this problem right away and release beta8!

> Yes, the problem is the `spyder/utils/site` directory didn't get packaged because of the lack of an `__init__.py`file in that dir (as @jitseniesen suggested ;-).

It was the comment by @hoogenm that put me on the right track.


Resolving patch:
diff --git a/spyder/utils/site/__init__.py b/spyder/utils/site/__init__.py
new file mode 100644
--- /dev/null
+++ b/spyder/utils/site/__init__.py
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © Spyder Project Contributors
+# Licensed under the terms of the MIT License
+# (see spyder/__init__.py for details)
+
+"""
+spyder.utils.site
+=================
+
+Site packages for Spyder consoles
+
+NOTE: This package shouldn't be imported at **any** place.
+      It's only used to set additional functionality for
+      our consoles.
+"""


Test patch:
diff --git a/continuous_integration/appveyor/modules_test.bat b/continuous_integration/appveyor/modules_test.bat
--- a/continuous_integration/appveyor/modules_test.bat
+++ b/continuous_integration/appveyor/modules_test.bat
@@ -24,6 +24,14 @@ for /r "%SPYDER%" %%f in (*.py) do (
         :: We don't want py.test's to be run here
         echo --- NOT testing %%f ---
         echo.
+    ) else if not "!file:site\=!"=="!file!" (
+        :: We can't test our site files
+        echo --- NOT testing %%f ---
+        echo.
+    ) else if not "!file:ipython\=!"=="!file!" (
+        :: We can't test these files outside of our IPython console
+        echo --- NOT testing %%f ---
+        echo.
     ) else if "%%f"=="%SPYDER%\utils\qthelpers.py" (
         echo --- NOT testing %%f ---
         echo.
@@ -54,18 +62,6 @@ for /r "%SPYDER%" %%f in (*.py) do (
         :: We have to investigate this failure!
         echo --- NOT testing %%f ---
         echo.
-    ) else if "%%f"=="%SPYDER%\utils\ipython\start_kernel.py" (
-        :: It can't be tested outside of a Qtconsole
-        echo --- NOT testing %%f ---
-        echo.
-    ) else if "%%f"=="%SPYDER%\utils\ipython\spyder_kernel.py" (
-        :: It can't be tested outside of a Qtconsole
-        echo --- NOT testing %%f ---
-        echo.
-    ) else if "%%f"=="%SPYDER%\utils\site\sitecustomize.py" (
-        :: It can't be tested outside of a Python console
-        echo --- NOT testing %%f ---
-        echo.
     ) else if "%%f"=="%SPYDER%\widgets\editor.py" (
         :: This is making AppVeyor to time out!
         echo --- NOT testing %%f ---