Related
So I'm trying to run python code from Sublime Text 3, but I'm not sure how. Even if it was only from the console, that would be fine. Anybody know how???
Tools->Build System->Python
or Ctrl+B
Need to install package to run python from sublime Python + Sublime
Try Anaconda plugin which will help you to run python on sublime
Setup Sublime for python
You can use this package in sublime text: https://packagecontrol.io/packages/Terminal to open a terminal at the specific file or folder.
Sublime Text 3 will run your python code inside the integrated console when you use Ctrl + B
if you want to run your code at own terminal, but still get some error information inside to integrated console, you need to build your own builder, or use plugins.
One way: https://github.com/Wilhox/Integrated-builder.git
This answer is for fellow googlers who want to run python scripts within their sublime. As other answers explains, all you need is a sublime build system, here after a bit of struggle I got it worked for Linux Systems.
{
"cmd": ["gnome-terminal", "--", "/bin/bash", "-c", "python3 -u \"$file\" echo;echo;echo Press Enter to exit...;read"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
This is by far the simplest I believe. Hope it helps.
If you need non-interactive Build System, just follow the Official Guide.
If you plan to run code that contains something like input(), or you have any other way of interacting with your program you would need additional setup - Plugin + simple config.
The steps of having proper/full build system are:
Install "Package Control":
Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Install Package
Control ▶ ENTER
Install Terminus via Package Control:
Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Package Control: Install Package ▶ ENTER ▶ Type: Terminus ▶ ENTER
Create Build System for Python
Tools ▶ Build System ▶ New Build System… menu item or the Build: New Build System ▶ Paste one of the following sections and edit respectively.
For Windows, obviously you should change the path to your Python:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -m py_compile \"${file}\"",
}
]
}
For Mac/Linux, do not forget to change the path to your Python.:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -m py_compile \"${file}\"",
}
]
}
Name the file e.g: Python Custom.sublime-build
Select the newly created Build System:
Tools ▶ Build System ▶ Python Custom
6. Execute your code:
Ctrl/CMD + B
SOURCE
#Thayakorn Rakwetpakorn's answer is correct
Ctrl+ B, and also make sure to have saved the file as hello.py or something before trying to run it
If that doesnt work
Tools->Build system -> New build system
Delete the existing code and paste the code I have shown below
{
//"shell_cmd": "make"
"cmd": ["C:\\Users\\Programs\\Python\\Python37\\python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Then change the file location to where your python.exe file location is, in the above code
"C:\\Users\\Programs\\Python\\Python37\\python.exe"
Instead of the code lines of my file location path
My question is, can Sublime 3 be setup to run the code that has been written with in Sublime. I've have searched on here and the internet and have tried numerous different suggestions. If the answer has already been posted and someone could point me in the proper direction / URL I'd appreciate it. If it cannot be done and you have other suggestion's I'll give it a try.
The following steps do work for Sublime Text 2 and 3. What you need is a so-called Sublime Text Build System which is represented by a valid JSON text file. There are a lot of Q and A to this topic on the internet. Anyway, here is a step-by-step list.
FYI: As far as I know there is no updated Syntax file for Python3.5 for Sublime Text. If anyone knows of an update, please let me know in the comment section.
Mac and Linux:
Open Sublime Text
In the menu bar go to Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. Verify that the path to your python 3.5 installation is correct, otherwise see step 4.
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.5/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
If you don't know the location of Python 3 try to execute 'which python3' in your terminal. Also verify that the correct python3 command is in your search-path. Here is some example output:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Save the file using (e.g using cmd (⌘) + s on your keyboard) and enter a filename like Python-3.5.sublime-build.
Now you can switch to your new build-system. By using (cmd + b) you can execute your Python script now.
Windows:
Open Sublime Text
In the menu bar go to Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. You need to verify that the path to python.exe is correct, otherwise update it.
{
"cmd": [r"C:\Python35\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save the file using (e.g using CTRL + s on your keyboard) and enter a filename like Python-3.5.sublime-build.
Now you can switch to your new build-system. By using (CTRL + b) you can execute your Python script now.
For python 3.6 i'm using:
{
"cmd": ["/usr/local/bin/python3.6", "$file"]
}
And everything works fine! Don't forget to get full path to your python interpreter.
Yes sublime can execute code in python3.
Sublime text executes the python file using system python available.
All you need to do is make python3 is default in your system. You can do this by adding setting PATH variable to point to python3.
To execute code:
Tools -> Build
CMD + B
Refer here How do I run Python code from Sublime Text 2?
By using the following code:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
would make the Sublime work.
Save the file as python3.sublime-build then select it in Tools -> build system -> Python3.
At last, run it by pressing command + B.
There are several online resources describing how to make SublimeText read PYTHONPATH variable specified in Windows Start menu > Control Panel > System > Advanced Settings > Environment Variables.
Yet, none of them clearly describe how to customize Sublime in Windows. How do we do it?
If using python 3.x you need to edit the Python3.sublime-build (Preferences > Browse packages > Python 3)
to look like this:
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env":{"PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"},
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
env needs to be a JSON object, or dictionary if you will, like this:
"env":
{"PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"},
My question is, can Sublime 3 be setup to run the code that has been written with in Sublime. I've have searched on here and the internet and have tried numerous different suggestions. If the answer has already been posted and someone could point me in the proper direction / URL I'd appreciate it. If it cannot be done and you have other suggestion's I'll give it a try.
The following steps do work for Sublime Text 2 and 3. What you need is a so-called Sublime Text Build System which is represented by a valid JSON text file. There are a lot of Q and A to this topic on the internet. Anyway, here is a step-by-step list.
FYI: As far as I know there is no updated Syntax file for Python3.5 for Sublime Text. If anyone knows of an update, please let me know in the comment section.
Mac and Linux:
Open Sublime Text
In the menu bar go to Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. Verify that the path to your python 3.5 installation is correct, otherwise see step 4.
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.5/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
If you don't know the location of Python 3 try to execute 'which python3' in your terminal. Also verify that the correct python3 command is in your search-path. Here is some example output:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Save the file using (e.g using cmd (⌘) + s on your keyboard) and enter a filename like Python-3.5.sublime-build.
Now you can switch to your new build-system. By using (cmd + b) you can execute your Python script now.
Windows:
Open Sublime Text
In the menu bar go to Tools -> Build-System -> New Build System
Paste the following code-snippet to the new opened file. You need to verify that the path to python.exe is correct, otherwise update it.
{
"cmd": [r"C:\Python35\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save the file using (e.g using CTRL + s on your keyboard) and enter a filename like Python-3.5.sublime-build.
Now you can switch to your new build-system. By using (CTRL + b) you can execute your Python script now.
For python 3.6 i'm using:
{
"cmd": ["/usr/local/bin/python3.6", "$file"]
}
And everything works fine! Don't forget to get full path to your python interpreter.
Yes sublime can execute code in python3.
Sublime text executes the python file using system python available.
All you need to do is make python3 is default in your system. You can do this by adding setting PATH variable to point to python3.
To execute code:
Tools -> Build
CMD + B
Refer here How do I run Python code from Sublime Text 2?
By using the following code:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
would make the Sublime work.
Save the file as python3.sublime-build then select it in Tools -> build system -> Python3.
At last, run it by pressing command + B.
I want to configure Sublime Text 3 to build Python 3, but I don't seem to understand how the builds work. Many tutorials have told me to make a build file containing code such as:
{
'cmd': ['/usr/bin/python3', '-u', '$file'],
'file_regex': '^[ ]*File "(…*?)", line ([0-9]*)',
'selector': 'source.python'
}
and save it as a file called Python.sublime-build or python3.sublime-build (much of the information I found was conflicting). One tutorial suggested creating a new folder in the ST3 Packages folder called Python and add the build file in there, whilst other tutorials suggested leaving it in the folder called User.
One tutorial explained how I had to change the Environment Variable path on my operating system to get it to work. That didn't seem to help either.
I added a folder Python to Packages (since it wasn't there already) and added in a build file with the name Python.sublime_build which featured only the code I posted above in it. Now when I attempt to run Sublime Text it gives me this error:
Error trying to parse build system:
Expected value in Packages\Python\Python.sublime-build:2:5
The reason you're getting the error is that you have a Unix-style path to the python executable, when you're running Windows. Change /usr/bin/python3 to C:/Python32/python.exe (make sure you use the forward slashes / and not Windows-style back slashes \). Once you make this change, you should be all set.
Also, you need to change the single quotes ' to double quotes " like so:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
The .sublime-build file needs to be valid JSON, which requires strings be wrapped in double quotes, not single.
Steps to Make Sublime Text a Python IDE (Windows)
Tested successfully on Sublime Text 3. Assuming Sublime Text and package control are already installed . . .
Install Python (python.org) and pay attention to where it is installed or choose a simple location like the C drive, agreeing to remove character limit at the end of the installation.
Install package SublimeREPL (Cntrl + Shift + P, Package Control - Install Package, SublimeREPL, Enter).
Go to Preferences, Package Settings, SublimeREPL, Settings - User.
Paste in the following, updating the file path to your python installation folder, as needed. You may customize these and choose whatever syntax you like (last line) but I prefer my output in plain text.
{
"default_extend_env": {"PATH":"C:\\Program Files\\Python36\\"},
"repl_view_settings": {
"translate_tabs_to_spaces": false,
"auto_indent": false,
"smart_indent": false,
"spell_check": false,
"indent_subsequent_lines": false,
"detect_indentation": false,
"auto_complete": true,
"line_numbers": false,
"gutter": false,
"syntax": "Packages/Text/Plain text.tmLanguage"
}
}
Save and close the file (SublimeREPL.sublime-settings).
Go to Tools, Build System, New Build System.
Replace all existing text with the following:
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Cntrl + S or save as "C:\Users[username]\AppData\Roaming\Sublime Text 3\Packages\User\SublimeREPL-python.sublime-build" updating username or path as needed. This should be wherever your settings and builds are stored by Sublime Text.
Go to Tools, Build System, select SublimeREPL-python.
All done--now to test. Open or create a simple python file, having a *.py extension and save it wherever desired.
Make sure the file is open and selected in Sublime Text. Now, when you press Cntrl + B to build and run it, it will open another tab, titled "REPL [python]", executing and displaying the results of your python code.
If you would like to go a step further, I highly recommend making the follow changes, to allow Sublime to reload your executed python in the same window, when you press Cntrl+B (Build), instead of it opening a new tab each time:
Add the following line in the "repl_python_run" command in (Preferences, Browse Packages) SublimeREPL\config\Python\Main.sublime-menu, right before the "external_id": "python" argument:
"view_id": "*REPL* [python]",
and then to change the line:
if view.id() == view_id
into:
if view.name() == view_id
in SublimeREPL\sublimerepl.py.
If you are using PyQt, then for normal work, you should add "shell":"true" value, this looks like:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell":"true"
}
Run Python Files in Sublime Text3
For Sublime Text 3,
First Install Package Control:
Press Ctrl + Shift + P, a search bar will open
Type Install package and then press enter
Click here to see Install Package Search Pic
After the package got installed. It may prompt to restart SublimeText
After completing the above step
Just again repeat the 1st and 2nd step, it will open the repositories this time
Search for Python 3 and Hit enter.
There you go.
Just press Ctrl + B in your python file and you'll get the output.
Click here to see Python 3 repo pic
It perfectly worked for me. Hopefully, it helped you too.
For any left requirements, visit https://packagecontrol.io/installation#st3 here.
Steps for configuring Sublime Text Editor3 for Python3 :-
Go to preferences in the toolbar.
Select Package Control.
A pop up will open.
Type/Select Package Control:Install Package.
Wait for a minute till repositories are loading.
Another Pop up will open.
Search for Python 3.
Now sublime text is set for Python3.
Now go to Tools-> Build System.
Select Python3.
Enjoy Coding.
Version for Linux. Create a file ~/.config/sublime-text-3/Packages/User/Python3.sublime-build with the following.
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
And to add on to the already solved problem, I had installed Portable Scientific Python on my flash drive E: which on another computer changed to D:, I would get the error "The system cannot find the file specified". So I used parent directory to define the path, like this:
From this:
{
"cmd": ["E:/WPy64-3720/python-3.7.2.amd64/python.exe", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
To this:
{
"cmd": ["../../../../WPy64-3720/python-3.7.2.amd64/python.exe","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
You can modify depending on where your python is installed your python.
first you need to find your python.exe location, to find location run this python script:
import sys
print(sys.executable)
Then you can create your custom python build:
{
"cmd": ["C:\\Users\\Sashi\\AppData\\Local\\Programs\\Python\\Python39\\python.exe", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"}
You can change the location, In my case it is C:\Users\Sashi\AppData\Local\Programs\Python\Python39\python.exe
Then save your new build. Don't change the file extension while saving.
I'd like to add just one point to the accepted answer:
when you edit the cmd portion of the snippet below, make sure to add the file address (with forward slash) where python is kept on your computer.
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
How to know where python is installed on your system? Two ways for windows users:
Open Command prompt and do the following query: where python
If it shows the path to python.exe, copy and paste it with /. If it shows some error, then follow process below:
Go to start -> Search for python shortcut (not IDLE) -> Right Click -> More -> Open file location -> Right click the shortcut -> Properties -> Target -> Path is now visible -> Copy and paste it in the field above.
Here is a very simple Python Sublime Text build system that works when python scripts are invoked with py file_name.py.
Just create py.sublime-build by Tools > Build System > New Build System and add the contents below:
{
"cmd": ["python3", "$file"]
}
You can select it in Sublime Text editor by going to Tools > Build System > py and building with Ctrl + b.
Note: If your filesystem doesn't python3 than you need to provide path/to/python3 and it should work.