How do I run python 3.5 in Sublime Text 3 - python

I have installed the python 3.5 interpretor in my device (Windows).
Can anybody guide me through the process of using packages to run it like SublimeREPL?

Yes, you can use any Python version you want to run programs from Sublime - you just need to define a new build system. Select Tools -> Build System -> New Build System, then delete its contents and replace it with:
{
"cmd": ["C:/Python35/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
assuming that C:/Python35/python.exe is the correct path. If python.exe resides someplace else, just put in the correct path, using forward slashes / instead of the Windows standard backward slashes \.
Save the file as Packages/User/Python3.sublime-build, where Packages is the folder opened by selecting Preferences -> Browse Packages... - Sublime should already automatically save it in the right directory. Now, there will be a Tools -> Build System -> Python3 option that you can select for running files with Python 3.
For details on setting up SublimeREPL with Python 3, please follow the instructions in my answer here.

if you have installed python3 and SublimeREPL, you can try setting up key bindings with the correct path to the python3 file.
[
{
"keys":["super+ctrl+r"],
"command": "repl_open",
"caption": "Python 3.6 - Open File",
"id": "repl_python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["The directory to your python3.6 file", "-i", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
You can try by copying this code into your /Sublime Text 3/Preferences/Key Bindings/
Hope this helps!

Related

How to run python code in Sublime Text 3?

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

Set PYTHONPATH in Sublime 3

I'm using Sublime editor for writing Python programs, with the Anaconda plugin.
I have a Python project which relies on some custom python lib I wrote in another directory.
I first modified the MyProject.sublime-project file in the build_systems section, it seemed to work but after some time, the sublime-project file is automaticcally overwritten.
I then found that the right way to do it is to add a settings/extra_paths section (from here) and my sublime-project file looks like this :
{
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"python\" -u \"$file\""
}
],
"folders":
[
{
"path": "."
}
],
"settings":
{
"extra_paths":
[
"C:\\path\\to\\my_lib"
]
}
}
But it doesn't work. Not only my libs import aren't found by the Python interpreter, but if I type :
import sys
print sys.path
I can't see my directory listed.
What am I doing wrong ?

Keybinding for SublimeREPL to launch a Python virtualenv

I am using Sublime Text 2 on a Mac. I have the SublimeREPL enabled and I am am trying to create a keybinding that that launches a REPL window for a virtual environment located in my virtualenvwrapper folder, ~/Documents/PythonEnvs/
I've tried modifying the code for my keybindings using
Running Python interactively from within Sublime Text 2
as a starting point, followed by
Sublime text3 and virtualenvs
and Running Python interactively from within Sublime Text 2
I'm probably missing something completely obvious, but it's actually less and less clear to me now if I need to be writing a plugin, a macro, or (what I thought) just writing a simple key binding to launch the SublimeREPL for Python using a virtual environment.
Has anyone written a keybinding to launch Python in a virtualenv? If so, how did you go about doing it?
The following is in my keybindings file.... it launches a new window that immediately reports "REPL CLOSED." The code doesn't work, or even come close... the best I've managed to do is get it to launch a current Python file in the Python env that comes with Sublime.... but in the interest of showing what I've tried, I've tried modifying my user's sublime-keymap file (latest iteration of that modifying below) for about an hour and a half now... and I can't anything online related to launching a SublimeREPL window in a a VirtualEnv, or anything similar enough that I can figure out how to solve this problem. At this point I gotta throw in the towel and see if anyone else has managed to do it. Thank you in advance for your help, if you have any ideas.
[
{ "keys": ["command+shift+p"], "command": "repl_open",
"caption": "Python - virtualenv",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "Python - virtualenv"
}
}
]
This should work:
[
{
"keys": ["command+shift+p"],
"command": "repl_open",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
To get this, I simply checked what sublimeREPL used itself.
Use "args": on line 19 of ~/Library/Application Support/Sublime Text 2/Packages/SublimeREPL/config/Python/Main.sublime-menu for the args in Key Bindings -- User
Side note:
To switch from version 2.7 to 3, replace
"cmd": ["python", "-i", "-u"],
with
"cmd": ["python3", "-i", "-u"],

How to Run Python Code on SublimeREPL

I really like using sublime text 2 to write Python codes, however any time I try to run a script which has an input, the sublime text console reports an error. So, I decided to try SublimeREPL, however I've been searching for hours and I didn't find out how to run Python code...
could you guys help me?
I want to run the code on SublimeREPL as we do with the sublime text console (CTRL+b).. what I actually want to know is whether or not there's a way to do the same with SublimeREPL.
Thank you in advance!
As described here, create a new Build System file and save it as ..\Packages\User\SublimeREPL-python.sublime-build. The file should contain:
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Then go to your Python file tab and select Tools > Build System > SublimeREPL-python. Now, Ctrl + B should execute the current Python file, with the output in a new tab. If you use a two column layout, the REPL output should open in the second column. (This was using Sublime Text 3.)
First "Install Package Control" from https://sublime.wbond.net/installation#st2
Optional(To check the above package is successfully installed:
Click the Preferences > Browse Packages… at this folder
Click Back Button one time and then into the Installed Packages/ folder, check there will be Package Control.sublime-package file)
then go to Preferences > Package Control > Package Control: Install Package in sublime text 2
find SublimeREPL in list.
Restart SublimeText2
open Preferences > Package Settings > SublimeREPL > Settings - Default file copy all text from there.
then open Preferences > Package Settings > SublimeREPL > Settings - User and paste the text here.
Restart SublimeText2
Go to Tools > SublimeREPL > Python > Python
And you are done
Steps to make Sublime Python Console which is Interactive and Reusable :
1) Install SublimeREPL plugin :
In Top Bar > "Tools" > "Command Palette" > "Package Control: Install Package"
Search for : "SublimeREPL" and install
2) Create Build System :
In Top Bar > "Tools" > "Build System" > "New Build System"
Replace all contents of the file with :
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Save the file as "PythonRepl.sublime-build" in the default "user" folder.
3) Settings to make Console interactive and Reusable:
|=> Goto "Preferences" > "Browse Packages"
|=> Goto Folder : SublimeRepl
|=> Edit : sublimerepl.py
Replace : if view.id() == view_id
With : if view.name() == view_id:
|=> Goto Folder : SublimeRepl/config/Python
|=> Edit : Main.sublime-menu
|=> Under "caption": "Python - RUN current file"
|=> Append : "-i", in "cmd" as :
"cmd": ["python", "-u", "$file_basename"],
"cmd": ["python", "-i", "-u", "$file_basename"],
|=> Add : Before "external_id": "python"
"view_id": "*REPL* [python]",
|=> Full Code as shown below :
--------------------------------------------------
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"view_id": "*REPL* [python]",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
4) Using :
4.1) Open the Python file that you want to run in Sublime Text.
4.2) In Top Bar > "Tools" > "Build System" > "PythonRepl".
4.3) Build the Python file, by choosing In Top Bar > "Tools" > "Build"
or
Using either the build shortcut (Ctrl+B for Windows, or ⌘ Command+B for Mac)
I want to expand on #sblair's response. #alexpmil asked in a comment how to prevent the REPL from closing.
In Sublime, go to Sublime Text > Preferences > Browse Packages
In your packages, open SublimeREPL\config\Python\Main.sublime-menu.
Find the part that contains id: repl_python_run.
Under args/cmd, add -i. That's it.
For reference, mine looks like the following:
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "d",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["C:/Python34/python", "-u", "-i", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}

Setting python path for sublimerepl from sublime text?

I use python portable along with sublime portable of a memory stick and decided to install sublime REPL however when trying to use python portable as the build path for sublimerepl it does not specify it.
To put it simply I can't specify the sublimerepl python path like I can with straight sublime text!
I have tried adding the following into SublimeREPL.sublime-settings
"default_extend_env": {"PATH": "{PATH}:C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe"},
I have also tried changing:
"cmd": ["python", "-i", "-u"],
to
"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file"],
within Python/Main.sublime-menu
Note in normal sublime I add the following to Python.sublime-build:
{"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file"],"file_regex": "^[ ]*Iile \"(...*?)\", line ([0-9]*)","selector": "source.python"}
Any ideas?
Thanks,
Andrew
Is it possible you're changing the wrong line of code in /Packages/SublimeREPL/config/Python/Main.sublime-menu? If you wish to run REPL on the current file,
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "d",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
is what the modified section should look like. (It appears you might be modifying the command which opens the interpreter in interactive mode as opposed to running the current file).
Thanks to Hower... I installed the package control, then the sublimePREPL package and, then in the keybinding (under preferences) I wrote this int the user file:
[
{"keys": ["ctrl+b"], "command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "d",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["C:\\Users\\giova\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}}
]

Categories

Resources