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"}
}
}
Related
as you see "python --version show python3.10.4
but the interpreter show python 3.7.3
how can i change the envirnment in vscode
If you click on the interpreter version being used by VSCode, you should be able to select different versions across your device.
Selecting the interpreter in VSCode:
https://code.visualstudio.com/docs/python/environments#_work-with-python-interpreters
To run streamlit in vscode:
Open the launch.json file of your project.
Copy the following:
{
"configurations": [
{
"name": "Python:Streamlit",
"type": "python",
"request": "launch",
"module": "streamlit",
"args": [
"run",
"${file}"
]
}
]
}
Adding the following line to your setting.json (crtl+shift+P "preferences: open settings(JSON)").
"terminal.integrated.env.osx": {
"PATH": ""
}
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.
For starters, here is my dev environment:
Windows 7 (although I have the same issue on another machine that is Windows 10)
Python 3.6
Git Bash
Sublime Text 3 (version 3.1.1, Build 3176)
SublimeREPL
In Git Bash, I created a new virtual environment:
$ mkdir ~/.venv
$ cd ~/.venv
$ python -m venv test-env
To activate that virtual environment, I use:
$ source ~/.venv/test-env/Scripts/activate
NOTE: I had to modify the activate script (NOT activate.bat) to get the venv to activate properly. Specifically, I changed line 40 which looked something like:
VIRTUAL_ENV="C:\Users\my_user_name\.venv\test-env"
to
VIRTUAL_ENV="/c/Users/my_user_name/.venv/test-env"
Now, when I am in the test-env virtual environment (as evidenced by the "(test-env)" text in Git Bash), I can do the usual stuff like
(test-env)
$ pip install numpy
I also have the SublimeREPL package installed in Sublime Text 3. I setup a new build system (SublimeREPL-python.sublime-build) that looks like:
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Now, suppose I have a script
# test.py
import numpy as np
print('numpy was imported without error')
I can type Ctrl+Shift+B, then start typing 'repl', which autoselects the SublimeREPL-python build, then hit Enter. The SublimeREPL appears, but generates an error:
Traceback (most recent call last):
File "test.py", line 2, in <module>
import numpy as numpy
ModuleNotFoundError: No module named 'numpy'
>>>
SublimeREPL was called without using my virtual environment, which causes it to throw the error because numpy wasn't installed in my global python environment.
How can I run my Python script from Sublime Text 3 using SublimeREPL and accessing my virtual environment that was created using venv?
FWIW, I already tried creating a Sublime Project for this code and adding the following to the .sublime-project file:
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "test-env",
"selector": "source.python",
"shell_cmd": "\"C:\\Users\\my_user_name\\.venv\\test-env\\Scripts\\python\" -u \"$file\""
}
]
This allowed me to type Ctrl+Shift+B, then "test-env", then Enter (to build with the test-env build system I just created), which worked as expected and ran without error. However, it does not use SublimeREPL, which I'd like so that I can debug my code (which is more complicated that the simple test script I posted above!) and explore the variables in the REPL rather than just running code in the console.
I know it's not an answer, but a partial solution that might help anyone else on same situation as I am. Also because SublimeREPL support is almost nothing.
Note: This solution requires to modify Main.sublime-menu for every environment and assumes SublimeREPL runs interactively already (adding the "-i" for execution).
Browse packages and open SublimeREPL/config/python/Main-sublime-menu.
Search for line with "id": "repl_python_run".
Duplicate the nearest content inside the curly brackes.
Basically pasting after the same end bracket the following (note the "-i" option on "cmd" to run interactively):
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-u", "-i", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
Change the id repl_python_run to whatever you like, e.g., repl_my_first_env_python.
Change python commmand from "cmd": ["python", "-u", "-i", "$file_basename"] to use wherever your python's virtual environment executable is, e.g., /home/me/.virtualenvs/my_first_env/bin/python (linux example). Save the file.
Edit project to include inside the square brackets from "build_systems" the following:
(If you have no project, create a new Build System and paste next block of code)
{
"name": "my first env",
"target": "run_existing_window_command",
"id": "repl_my_first_env_python",
"file": "config/Python/Main.sublime-menu"
},
Finally Save.
You'll see when Ctrl+Shift+B you'll see my first env as an option.
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 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"],