Sublime Forum

Please Help: custom build of Python 3.7.1 not working

#1

So, I’m relatively new to Sublime and sort of a novice in general. I’ve been using Sublime for web development and am looking to expand my use of Sublime to Python.

I’ve tried all sorts of variations of the form:

{
“cmd”: [“TARGET”, “-u”, “-i”, “$file”],
“file_regex”: “^[ ]File "(…?)”, line ([0-9]*)",
“selector”: “source.python”
}

Sometimes I include the “-i”, other times I don’t. Depends on who’s tips I’m following when. I always make sure to save the files as a ‘.sublime-build’ as well.

I’ve tried using “python” as TARGET (the only version of Python I have installed is 3.7.1) and all sorts of variations of file paths, the latest being to use ‘where python’ on the Windows command line to locate the exact file path to use as TARGET (“C:\Users\rnewe\AppData\Local\Programs\Python\Python37-32\python.exe”)

I had a working build at one point on a test script, but never since. All I get now are ‘No build system’ notifications. As soon as I switch over to my real script, I get the same thing.

Everything I’ve seen thus far has lead me to believe the following should give me a build of Python 3.7.1

{
“cmd”: [“C:\Users\rnewe\AppData\Local\Programs\Python\Python37-32\python.exe”, “-u”, “-i”, “$file”],
“file_regex”: “^[ ]File "(…?)”, line ([0-9]*)",
“selector”: “source.python”
}

Can anyone offer some help?

I’m really hoping I’ve just got a simple mistake somewhere.

0 Likes

#2

When you see the error No Build System when you know you have selected one that should apply in the current situation, the general reason for that is because there’s an issue parsing the JSON in the sublime-build file, which stops Sublime from recognizing the contents of the file and then you get the error telling you there’s no build system.

In your case, the reason for this error (based on your last example) is that the JSON in your sublime-build file is not valid. If you install PackageDev (which despite the name is invaluable even if you’re not developing packages), it’s enhanced syntax for build files will highlight the problem for you:

In JSON strings, the \ characteris used to introduce a string escape character, telling the system that the following character should be interpreted specially. As seen in the image your windows path separators are being interpreted as escape characters.

In some cases, such as \r, it’s a valid escape sequence but won’t match your file unless your filename has a linefeed in it, where for the others, such as \U the escape code is invalid.

To fix that problem, you either need to double up the path characters, like C:\\Users\\rnewel\\... or convert them to posix path separators instead, like C:/Users/rnewel/....

Assuming that the path to Python is correct, that should be enough to fix your problem.

Generally, if where python in a command prompt tells you where python is, that’s an indicate that the location is on your PATH, so Sublime should find it just fine and you shouldn’t need to include the path at all. Note that Sublime captures the environment at the point at which it was first executed, so if you fiddle the environment while Sublime is running, you may need to quit and restart for your changes to be seen.

0 Likes

#3

Also, on Windows it is highly advised to use the py.exe Python launcher that is installed into your Windows folder. As such, it will be found even without a full path specifier. Furthermore, -i enables interactive mode, but Sublime Text can’t forward user input in build systems, so it’s useless (and might even cause problems).

Thus, change your cmd to

"cmd": ["py.exe", "-u", "$file"],
0 Likes

#4

Based on both of your advice I now have a build, which is awesome, so thanks!

I think the “-i” came from advice geared towards using REPL, it worked with and without, regardless I’m not using it now.

@FichteFoll I did a little reading into py.exe and it looks like py.exe runs a version of Python specified by the script with what Python calls a ‘shebang’? Is that right?

I’m trying to understand why I would want to use py.exe as opposed to FILEPATH\\python.exe, from my perspective it looks like I’m trading specifying a version/file path in the .sublime-build file or specify the version as a shebang/let py.exe decide which version to use if no shebang is used.

The Python documentation mentions not needing file paths for executables in “well known” directories, but the shebang lines look like file paths to me…

0 Likes

#5

See https://docs.python.org/3/using/windows.html#launcher for documentation about the py launcher on Windows.

TL;DR it allows dynamic selection of the Python version to be used from the command line and for scripts with shebang lines. Shebang lines are interpreted as if they were UNIX paths, so #!/usr/bin/env python3 would be recognized as the latest Python 3 version on your system. Similarly, it is also aware of virtualenvs. And writing py -3.7 is just much shorter than including the full path to your 3.7 executable.

Just remember ot always use the py launcher as it makes things a lot easier.
The only reason it’s not set by default in the build system is because only Python 3 versions include the launcher, Python 2.7 does not.

0 Likes

#6

That’s where I was grabbing my information, there and Reddit (they had a nice watered-down topic along the same lines). Now that I understand what’s actually going on the py launcher it seems like a no-brainer.

I’ve been playing around to get a 3.7 build but not having success and I have one last question regarding this if you’re willing to field it.

In the command prompt the following opens Python 3.7 as expected
c:/…/>py -3.7

The sublime build file as follows builds from the latest Python version installed (3.7.1)
{
“cmd”: [“py”, “-u”, “$file”],
“file_regex”: “^[ ]File "(…?)”, line ([0-9]*)",
“selector”: “source.python”
}

Once I modify it to the following
{
“cmd”: [“py -3.7”, “-u”, “$file”],
“file_regex”: “^[ ]File "(…?)”, line ([0-9]*)",
“selector”: “source.python”
}

I get this error:
[WinError 2] The system cannot find the file specified

I would think that what worked in the command prompt would work in the sublime-build file, but it doesn’t seem to be working that way… I’m also having trouble finding resources to point me in a better direction. Do you happen to have any suggestions?

0 Likes

#7

If -3.7 is supposed to be a command line argument, it needs to be it’s own string in the cmd list; otherwise the underlying call to run the program is trying to run literally a program named py -3.7 which probably does not exist. :slight_smile:

So something like:

"cmd": ["py", "-3.7", "-u", "$file"],
1 Like

#8

And lone behold that solves the issue, thanks a bunch to both of you!

Sorry if this all seemed trivial, I start to lose my way when I start to stray from just writing scripts. Build files are a bit clearer now and the py launcher/PackageDev were great suggestions

1 Like

#9

OdatNurd is right! I am a freshman.If you use windows, you can use the codings

{

	"cmd":["py", "-u",  "$file"],
	"path":"C:\\Python37\\python.exe",
    "file_regex":"^[]File\"(...?)\",line([0-9]*)",
    "selector": "source.python"

}

you can try it.

0 Likes