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"],
Related
I have recently downloaded Sublime Text 3 and since it does not have a Python shell, I have downloaded SublimeREPL, but every time I run the code by SublimeREPL >>> Python >>> Python - Run current file I get ***Repl Closed*** text after the output, which prevents me from interacting with the shell. Is there any way around this?
Well not 100% convinced by SublimeREPL, but let's get straight to the solution :
Find the Python configuration
~/.config/sublime-text-3/Packages/SublimeREPL/config/Python/Main.sublime-menu
add the -i flag to the python command in this section (you're also free to create a new one)
{"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",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
Now the interpreter stays open upon program completion.
Environment:
Windows 10 x64
VSCode Version 1.23.1
python 3.6.4
I've been trying to switch from sublime text 3 to VS code.
I have python 3.6.4 installed in my computer, all set with the PATH and other stuff.
In my sublime text 3, I set the build for python 3 (*.sublime-build) as:
{
"shell_cmd": "python --version && echo. && python -u \"$file\"",
"cmd": ["C:/Users/jxie0/AppData/Local/Programs/Python/Python36/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9])*",
"selector": "source.python"
}
this will add a python version output with a blank line, before running the python code, take print('hello world') as an example:
Python 3.6.4
hello world
[Finished in 0.1s]
And sublime automatic add a timer for the code at the end.
In my current task.json for VS code, I have:
{
"version": "0.1.0",
"command": "python",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
This will work, but just simply show the output of the python codes:
hello world
Is there anyway I can tune it into something that can give me same kind of output style as I got from sublime text? If the timer is not possible, at least can I add a shell command as a header to show the python 3 version?
Additional question: what does that "version": "0.1.0", do in VS code? Which version does it refer to?
Thanks!
You can use the same command as you were using before, the difference is in how the file to be passed is referenced. In vscode the equivalent would be
python --version && echo && python -u ${file}
This command can be broken up into three commmands, the first, python --version, prints the python version. The second, echo, prints the empty line. The third, python -u ${file}, actually runs the python script.
As for the timer, I don't know of a way to make that occur in vscode but it would not be too difficult to implement this in the script itself, such as in Measure time elapsed in Python?
A simple configuration for this task could be as follows:
{
"version": "2.0.0",
"tasks": [
{
"label": "python",
"type": "shell",
"command": "python --version && echo && python -u ${file}",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
]
}
Note that this assumes you have python on your path, otherwise you will have to specify the path to python.exe when the python command is used in command.
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!
I'm trying to run Python on SublimeREPL, but python is not on interactive mode.
So when I send a line to sublimeREPL, it just shows something if it's an error or if I print something.
How can I change python to interactive mode?
Found a solution:
After setting up Python on SublimeREPL, go to Keybindings - User and add this code below. It will be just like the normal PythonIDE!
[
{ "keys": ["f8"], "command": "repl_open",
"caption": "Python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python"
} }
]
I'm not understanding your problem with that? Python will only log data to the console if you print something or it encounters an error, there is nothing else to show usually.
Regardless, Python should be preconfigured on SublimeREPL. Look at the documentation, and if that doesn't help then look at this guide. However it doesn't sound like there's anything going wrong.
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"}
}
}