Hi all,
I know this question has been asked multiple times but I can’t make it work: I have used sublime for years and never faced this problem.However after installing ST4 I see this error popping up. I have python installed through miniconda on a windows 10 machine. My system seems to be unable to recognize the command “py” (if I run “where py” doesn’t find anything). If I create a new build pointing to the python executable in the miniconda folder I can build simple scripts, however the graphics doesn’t work (as in I can do most things but I can’t plot things through matplotlib, for example). Everything works fine outside of ST. Any help is much appreciated.
Thanks.
[WinError 2] The system cannot find the file specified + I can't plot
TL;DR
The default Python build system changed how it invokes Python, so if you’re not using a “standard” Python installation on Windows you need a custom build to execute it (as you’ve noticed), and your custom build must either use shell_cmd
OR cmd
in concert with "shell": true,
if what you’re executing needs to open some external window (as MatPlotLib most likely does).
The Longer Deets and Explanations
The Python.sublime-build
that ships with ST3 is:
{
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python -m py_compile \"${file}\"",
}
]
}
The Python.sublime-build
that ships with ST4 is:
{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"windows": {
"cmd": ["py", "-u", "$file"],
},
"variants":
[
{
"name": "Syntax Check",
"cmd": ["python3", "-m", "py_compile", "$file"],
"windows": {
"cmd": ["py", "-m", "py_compile", "$file"],
}
}
]
}
The main differences here are:
- It uses
cmd
instead ofshell_cmd
- On Linux/MacOS the command
python3
is used instead ofpython
- On Windows, the command
py
is used instead ofpython
The reason for #2 is that on those operating systems, python
represents Python 2.7 but most people are trying to write code with Python 3.whatever. The interpreter names (as defined by Python itself) are different to allow both versions to be installed simultaneously since they are not directly compatible, and one of the most commonly asked questions about Sublime Text and Python relates to the wrong version of Python being executed.
The reason for #3 is that the second most common problem mentioned regarding Sublime Text and Python on Windows is that unless you tick the box in the Python installer, it won’t put the Python install directory on the PATH
, which stops many people from being able to execute Python at all. However, the Python installer puts a py.exe
helper in the Windows folder, which is always on the PATH
, and that program knows how to find the appropriate Python and run it.
Thus for people that rely on the default build but didn’t check the box or don’t know how to manipulate the PATH
, things still work. However, it would appear that not all Python distributions do that, which is the source of one of your problems.
The reason for #1? Well, that I’m not entirely sure about. However, it’s the most likely cause of why your matplotlib
program doesn’t do anything, as weird as that may seem.
Without getting into the minutiae of the internals of the Python standard libraries, (though you could certainly dig into that if you care), Sublime is using a standard Python library to execute the program in the build, and that library, when given a cmd
, tells Windows not to open any additional windows while running the program (i.e. it runs in the “background”, if you will, only able to send text output to the console).
You can visualize this by using the Sublime Console to execute the following three things, in turn:
window.run_command('exec', {'shell_cmd': 'notepad'})
window.run_command('exec', {'cmd': ['notepad.exe']})
window.run_command('exec', {'cmd': ['notepad.exe'], 'shell': True})
The shell_cmd
version will execute Notepad, and a window for Notepad will open. This simulates actually entering notepad
into a terminal.
The first cmd
will run Notepad, but you can’t see the window at all. This is what’s likely happening to your matplotlib
; it’s trying to create a window, but Python told windows not to allow it to do so.
The third cmd
includes the shell
line that tells the Python library to treat cmd
as the shell_cmd
version (i.e. pass the command to the command prompt as if you typed it), and now the Notepad window appears as expected.
So the lesson is to either use a build with shell_cmd
in it, or add "shell": true
to the build. If you use a build like the ST4 default, you only need to put it in the top level build, not inside of the variant
; the variant
will pick it up directly.
Some application Nothing showing on console after new build
Thank you for your very detailed answer, this now works
Stupid question: is there a way to modify the default python build or should I avoid doing so?
Thanks
There is (by creating an override
) if you want to go that route.
If you install OverrideAudit, you can use OverideAudit: Create Override
from the command palette, select Python
then Python.sublime-build
.
That will open the file for editing; if you save the file an override
will be created, which basically makes Sublime ignore the version of the file that it ships with, using your modified version instead.
Such a change will remain in place until you remove the file, including if Sublime updates and a newer Python
package changes the build. In the normal case that would be bad, but OverrideAudit
will tell you if that happens so you’re not surprised.