Python 3.4 on Sublime Text 3 - python-3.x

I followed these steps to run Python 3 on Sublime Text 3.
Select the menu Tools > Build > New Build System and I entered the following:
{
"cmd": ["python3", "$file"]
, "selector": "source.python"
, "file_regex": "file \"(...*?)\", line ([0-9]+)"
}
After that, I saved it to the following (Mac-specific) directory: ~/Library/Application Support/Sublime Text 3/Packages/User
but I'm getting this error when I'm trying to run my code on Python 3 in Sublime:
[Errno 2] No such file or directory: 'python3'

You need to provide the full path to python3, since Sublime Text does not read your ~/.bash_profile file. Open up Terminal, type which python3, and use that full path:
{
"cmd": ["path/to/python3", "$file"],
"selector": "source.python",
"file_regex": "file \"(...*?)\", line ([0-9]+)"
}

This is the snippet that I've been using. It's a slight variation to Andrew's solution, such that python3 is dynamically located by consulting the UNIX environment's PATH setting (not unlike how you would do the same inside a Python shell script; e.g.: '#! /usr/bin/env python3').
This snippet also uses "shell_cmd" instead of "cmd", which sublime-text-3 has seemingly switched to.
{
"shell_cmd": "/usr/bin/env python3 ${file}",
"selector": "source.python",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"working_dir": "${file_path}",
}
I saved mine in ".../Packages/User/Python3.sublime-build". I hope this helps you. =:)

Thanks for your question. I began to learn python a couple days ago, and I am stuck with the same problem that you have met.Just as Andrew said above,it is “path problem”. I would like to share the code that I used to get python3 on sublime3.
For MacOS user:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
"path": "/usr/local/Frameworks/Python.framework/Versions/3.3/bin/"
}
and save the file as Python3.sublime-build.
I deeply recommended the book "A byte of python " for the new beginner of python.This book contributes a lot to my answer for this question.

Related

No Code output in sublime text 3, C++. I have tried the solution in previous posts

When there is no error in my code, I get this message in sublime
as you can see just build time but no output
I have added the mingW/bin folder to my path variable.
Here is my build system-
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
Do I need to make any changes to this? If yes, please let me know.
ALso when I press ctrl+b does it compile the entire .cpp file or does it only compile the current line?
Please help a fellow computer enthusiast out, I have looked it up on stack overflow, sublime forum but I cannot seem to find any answer out there.

Python3 Sublime Text3 Not showing the input value as output

I am using sublime text3 for Python on Ubuntu 18.04. But I can't print anything when I am taking input. Like I can print this
print("Hello")
but I can't print this
test= int(input())
print(test)
I give input but it doesn't show any output.
My python build code is
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
I have followed this Sublime Text 2 console input
But not working. Here is the screenshot using SublimeRELP package
In this problem, I use sublimeREPL as like this. But again this problem occurs. Accidently I found that I was pressing my num Enter but it worked with my general Enter button. After pressing the general Enter button, it works fine.

Sublime 3 Path Variables

I am trying to set up a python 3 build path for sublime text 3 on a windows 10 pc. The system says that it cannot find the specified file, but I have already added the path and set the build system for python3. The error message I get when I try to build is below..
[WinError 2] The system cannot find the file specified
[cmd: ['python3', '-i', '-u', 'C:\\Users\\strinkjr\\Desktop\\Python Stuff\\errorSearch.py']]
[dir: C:\Users\strinkjr\Desktop\Python Stuff]
[path: C:\Users\strinkjr\Desktop\Python Stuff\]
[Finished]
My build environment file is as follows:
{
"cmd": ["python3", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/python.exe\", line ([0-9]*)",
"selector": "source.python"
}
I am unsure if I set up the path incorrectly or if I set up the build environment incorrectly. (Maybe both)
There are a couple of overall problems you're having that are standing in your way here.
The first is that you didn't set the PATH correctly. The build output shows you the PATH as it's currently defined as far as the command execution is concerned:
[path: C:\Users\strinkjr\Desktop\Python Stuff\]
The PATH is the list of locations where windows will look for the program that you're trying to execute, so unless there is a python3.exe in this directory you've accidentally set the PATH to the location of the files that you're running and not the interpreter that's used to run them.
Secondly you're passing -i to the Python interpreter to get it to drop into interactive mode once it's done executing the script. Sublime doesn't let you interact with programs that you execute from within a sublime-build, so if you do this once your program finishes executing and goes into interactive mode, it's going to be effectively hung waiting for you to provide it input that you can't provide.
Your build system also contains this file_regex entry:
"file_regex": "^[ ]File \"C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/python.exe\", line ([0-9]*)",
In a sublime-build file, the file_regex is used to be able to detect what lines in the program's output are errors so that Sublime can allow you to navigate between errors or flag them with inline errors if you have that option turned on.
Although this won't stop your programs from running, it will stop Sublime from being able to recognize errors because the name of the file is never going to match.
I would try a sublime-build file something like the following and see if that works better for you:
{
"shell_cmd": "python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {
"PYTHONIOENCODING": "utf-8",
"PATH": "$PATH;C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/"
},
}
This removes the -i argument to stop the interpreter from going interactive in order to stop any problems and uses shell_cmd instead of cmd to provide the command, which changes the format slightly.
The file_regex here is one that will match regular python errors, which is similar to the one you already provided but without the reference to the Python executable.
The big addition here is a couple of environment variables. The first one ensures that Python knows that it should use utf-8 to generate output, since that's what the Sublime console is expecting it to use. That stops you from getting potential errors if you try to display non-ascii output.
This also applies a new PATH that includes the existing path and also adds to it the path that looks like it might be where your Python is installed based on the files you were already using.
That part may need adjustment if the location is not correct; alternatively you can remove the PATH portion of the sublime-build and modify your PATH environment variable as appropriate instead. Note that you may need to restart Sublime if you do that in order for it to see the change.

How to create Sublime Text 3 build system which reads shebang

How can I create a build system in Sublime Text 3 where "cmd" is replaced with a shebang if it exists?
More specifically, is there a way to alter the Python build system to use the version of Python specified in the shebang, and use a default if no shebang is present?
Sublime build systems have an option named target which specifies a WindowCommand that is to be invoked to perform the build. By default this is the internal exec command. You can create your own command that would examine the file for a shebang and use that interpreter or some default otherwise.
For example (caveat: I'm not super proficient in Python so this is probably quite ugly):
import sublime, sublime_plugin
class ShebangerCommand(sublime_plugin.WindowCommand):
def parseShebang (self, filename):
with open(filename, 'r') as handle:
shebang = handle.readline ().strip ().split (' ', 1)[0]
if shebang.startswith ("#!"):
return shebang[2:]
return None
def createExecDict(self, sourceDict):
current_file = self.window.active_view ().file_name()
args = dict (sourceDict)
interpreter = args.pop ("interpreter_default", "python")
exec_args = args.pop ("interpreter_args", ["-u"])
shebang = self.parseShebang (current_file)
args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter,
" ".join (exec_args),
current_file)
return args
def run(self, **kwargs):
self.window.run_command ("exec", self.createExecDict (kwargs))
You would save this in Packages/User as a python file (e.g. shebanger.py).
This creates a new command named shebanger that collects the arguments it's been given, examines the file in the currently active view of the window the build is triggered in to see if the first line is a shebang, and then synthesizes the arguments needed for the exec command and runs it.
Since the default python build system assumes it is building the current file and passing -u as an argument, that's what this command replicates as well. Note however that this code is not 100% correct because any arguments in the shebang line will be ignored, but you get the general idea.
In use, you would modify the default Python.sublime-build file to look like this:
{
// WindowCommand to execute for this build
"target": "shebanger",
// Use this when there is no shebang
"interpreter_default": "python",
// Args to pass to the interpreter
"interpreter_args": ["-u"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"interpreter_args": ["-m py_compile"],
}
]
}
Notice that in the variant we override what the interpreter arguments are; you could also override the default interpreter there as well if desired.
If think the only way to do this using a standard .sublime-build file is to pass your file to another script which then parses the shebang and passes it on to the correct Python version.
Alternatively, you could specify build variants, but then you will have to choose the desired build variant manually.
My python.sublime-build
{
"cmd": ["py", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell":true
}
In windows I used py launcher to detect the versions according to the shebang

bash permission denied on C++ build&run chain keybiding

Through steps found:
Is it possible to chain key binding commands in sublime text 2?
Using the following Build System:
{
"cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants": [{
"name": "Run",
"shell": true,
"cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;read line;exit; exec bash\"'"]
}]
}
I created the following .py extension:
import sublime, sublime_plugin
class BuildAndRun(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("build")
self.window.run_command("build", {"variant": "Run"})
And keybiding:
{ "keys": ["ctrl+b"], "command": "build_and_run"},
The keybiding activates the extension correctly, but then in terminal it returns:
bash: /home/hadrian/Documents/new: Permission denied
'new' is the name of of .cpp file.
The problem is also that, if it only has (in the .py extension) build, it builds, if it only has run, it runs, but if it has both it returns that bash error.
I'm not being able to find out where this 'permission error' is created through the whole process.

Resources