Sublime Forum

Python File won't run from Sublime text [Errno 2]

#1

Hi, I’m fairly new to all of this and have been learning Python and have been using Sublime to do so.

Currently when I try to use the build system that I configured, i receive the following message:

Traceback (most recent call last):
File “/Users/patrickheath/Desktop/program_course/crash_course/chapter_10/10_1_files_readingcode.py”, line 3, in
with open(filename) as fileobject:
FileNotFoundError: [Errno 2] No such file or directory: ‘learning_python.txt’

I managed to do some research at the start so I could use the interactive terminal, and this has been working perfectly until now. This is the buildsystem configuration that has been working enabling the interactive terminal to work at the bottom of the screen.

{
“target”: “terminus_exec”,
“cancel”: “terminus_cancel_build”,

"focus": true,
"shell_cmd": "/Library/Frameworks/Python.framework/Versions/3.8/bin/Python3 -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}\"",
	}
]

}

Is there something simple in that I can change to enable it to run? I have tried some of the other build systems that I have found, these allow the file to open, but they are not interactive. I have been trying various different ways that I have found online, but these aren’t changing.

Many thanks

0 Likes

#2

You’re running afoul of the current working directory not being set correctly.

All running programs have the concept of a current directory or working directory, which is what folder on your computer is considered to be the “current” directory. This is something specific to the OS in general and not Sublime or Python in particular, and is used by the OS when you ask it to do things that require filenames that have a "relative" name.

A filename like /home/youruser/samplefile.txt or c:\Users\YourUser\Desktop\samplefile.txt is an example of an absolute filename; a filename that tells the OS exactly where to find a particular file.

On the flip side, a filename like samplefile.txt, ./samplefile.txt or ../something/samplefile.txt is an example of a relative filename; in order to work with such a file, the OS has to know what location on the disk the file is relative to so that it can turn it into an absolute filename to work with it; the location that paths are relative to is the current directory.

When you run a program interactively in the command prompt, whatever directory you’re sitting inside of when you run the program is the current directory, so if you’re in c:\users\someuser\desktop, that’s the current directory and trying to open something.txt would be assumed to be in that folder.

The current directory of a program is an inherited thing if it’s not explicitly stated. Above when you execute python interactively in a command prompt, it inherits the current directory from the thing that ran it (the command prompt).

The same is true of build systems in Sublime (both Terminus and “regular” builds); in the absence of any particular instruction, the thing you execute inherits what Sublime has as its current directory, which is almost certainly not where you expect it to be.

This is a really long winded way of saying that you want to add something like this to your build system:

"working_dir": "$file_path",

That would set the current directory to be whatever folder the python file you’re executing is in. Most programs such as the kind you’re likely executing are written to assume that the file they’re opening is stored in the same location as itself.

The Python.sublime-build file that ships with Sublime doesn’t have such a line in it, so using it as the basis for a Terminus build doesn’t have it either. Both Sublime and Terminus allow this key to be set.

Putting it in the top level of the build (e.g. between file_regex and selector) will make it available to the main build and also the variant, so that both builds will do the right thing.

0 Likes

#3

Thank you so much for such a kind and detailed reply! Amazing the addition of one line and it is all seems to be working now. As a real novice, diving into this stuff is really quite complicated for me, so it’s really good of you to take the time to clearly explain what is going on and give me a solution.

1 Like