Sublime Text 2 - SublimeREPL package issue - python

Basically, I've installed Sublime Text 2 to write in Python.
I then installed Package control and via package control I search "SublimeREPL" and I install the package, however even after installing, when I select Python through Tools > SublimeREPL > Python > Python, I get the error:
WindowsError(2, 'The system cannot find the file specified.')
Any help please? I'm pretty new to this.

Go to preferences -> Keybindings - User
and paste this. Make sure there are only one set of [] in your file, if you have custom keybinds set already then add a , after your last } then paste everything but the [] at the beginning and end after your ,.
[
{ "keys": ["ctrl+shift+r"], "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"
}
}
]
Save and quit Sublime Text.
Follow these instructions
How to add to the pythonpath in windows 7?
You should now be able to run your current file in repl with crtl+shift+r
Open sublime text and create a new file. Save it as hello.py
Type:
print('Hello World')
Save then hit ctrl-shift-r
See if that works

Related

Debugging python code that uses python package in vscode with launch.json

I'm trying to use the debugger from vscode for a specific python project. The terminal command that I use to run this python package is as followed.
synthtiger -o ./outputs/SynthDoG_adapt -c 5 -w 4 -v template.py SynthDoG config_address.yaml
The command uses a python package that can be installed by running pip install synthtiger and uses some inputs given by the arguments after the synthtiger command. The vscode launch configuration that im currently using is as followed.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: synthdog",
"type": "python",
"request": "launch",
"program": "synthtiger",
"python": "/home/XXX/anaconda3/envs/synthdog/bin/python",
"args": [
"-o",
"./outputs/SynthDoG_adapt",
"-c",
"5",
"-w",
"4",
"-v",
"-template",
"SynthDog",
"config_address.yaml"
],
"console": "integratedTerminal",
"justMyCode": true
}
]
}
If I then run the debugger from vscode, I get the following error.
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/d/projects/data_generation/synthdog/synthtiger'
Which indiactes that the python file is not found, which is also correct since I installed the python package with pip. The python package it builds upon is listed here. The default command that one can use to test this approach is detailed here, such as synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_horizontal.yaml and requires the github folder in the current directory.
The question I have is how do I adapt the launch.json to run the synthiger command instead of trying to search for a python file called synthtiger. I was hoping for a solution something in the range of changing the type to shell command.
Thanks for taking a look.

How do I set up VS Code for Python to correctly launch/debug individual files such that relative imports are respected?

I found this answer very useful to understand relative imports in Python. However I have found difficulty in getting VS Code's "Current File" to play nicely with this, since it launches the file directly using ${file}.
To summarise said answer, suppose we have a structure like this:
mymod/
__init__.py
apple.py
orange.py
If orange.py imports something from apple.py:
from .apple import apple_function
then the correct way to run orange.py as a script without an ImportError is with the following command from the directory containing mymod:
python -m mymod.orange
Currently, I manually enter a command similar to the above into the terminal, which works, but I'd prefer to use a keyboard shortcut that works regardless of filename and saves me some typing.
Is there anything I can add to the launch.json to have a configuration that launches python for each particular file as above automatically?
Use the command extension.commandvariable.file.relativeDirDots from the extension Command Variable
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}

SublimeREPL does not run

I used SublimeREPL happily, then I wanted "Open with SubilimeText" option to be on right-click. So I installed Sublime Text (without removing) with "Add to explorer context menu" checked. And now when I press F7 SublimeREPL does nothing.
The only way it runs Python files is
Tools > SublimeREPL > Python > RUN Current File
I tried to remove REPL.build-system, re-installing package. Doesn't help.
How can I fix it?
I have found the fix faster than you guys but thanks anyways :).
If anybody have the same problem this may help:
Open Sublime Text > Go to Preferences > Key Bindings
In "Key Bindings - User" tab paste the following code (Note: I set key "F7", but any other free key can be set):
[
{
"keys": ["f7"],
"command": "repl_open",
"args": {
"cmd": ["python", "-u", "-i", "$file_basename"],
"cwd": "$file_path",
"encoding": "utf8",
"extend_env": {"PYTHONIOENCODING": "utf-8"},
"external_id": "python",
"syntax": "Packages/Python/Python.tmLanguage",
"type": "subprocess"
}
}]

How can I access python shell in sublime text 3? [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 6 years ago.
Is it possible to set up sublime text for full stack python development. By this, I mean the following:
1. python shell where I can input an expression and instantly get the result,
2. the shell will work for the 'input' method, which will prompt the user to enter data.
I have installed the theme and color scheme, but the console/shell is not working or I did not get the correct package from the package control. I want an environment where I can work in the sublime text without shifting into the IDLE. Please help me kindly if you know how to do it.
There are several ways to do this:
As #cricket_007 states you can install SublimeREPL and run the current file via Tools >>> SublimeREPL >>> Python >>> Python - Run current file. This interprets the python file in a Sublime Text view and inputs are possible. However since this is not a build system you can create a build system from this (click Tools >>> Build System >>> New Build System... and paste):
{
"target": "repl_open",
"encoding": "utf8",
"syntax": "Packages/Python/Python.tmLanguage",
"extend_env": {"PYTHONIOENCODING": "utf-8"},
"type": "subprocess",
"cmd": ["python", "-u", "$file"],
"cwd": "$file_path",
"external_id": "python",
"selector": "source.python"
}
As an alternative one could run the file in a terminal, by using the following build system. This will create a new terminal and execute the file in python and afterwards await pressing enter.
{
"selector": "source.python",
"windows": {
"shell_cmd": "start \"$file_name\" cmd /c \"python $file_name & pause\""
},
"linux": {
"shell_cmd": "xterm -T \"$file_name\" -e bash -c \"python $file_name; echo Press enter to exit... & read\""
}
}
Feel free to add the osx option

sublime text 2 console and python 3

I am able to set python3.2 for the build command in sublime text 2(and build with python3.2), but when invoking the console with cmd-` the interpreter is mac's default 2.6 version.
Any help is greatly appreciated!
The console in Sublime Text 2 uses the internal version of Python, which is 2.6. There is no way to change it without breaking a whole bunch of stuff. There is a workaround, though. If you just want a Python console within ST2, use the awesome SublimeREPL package, which can also be installed through Package Control. Among many other things, you can send selections or whole files to be interpreted through a REPL of your choice, including Python 3. Create Packages/User/SublimeREPL/config/Python/Main.sublime-menu with the following contents:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
{"caption": "Python",
"id": "Python",
"children":[
{"command": "repl_open",
"caption": "Python 3",
"id": "repl_python3",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
}
]
}]
}
]
changing the "cmd" option to the full path to your python3 binary. This way your changes will survive any SublimeREPL upgrades. BTW, this path works for any package, so you can feel free to customize away without fear of accidentally losing it all.
Cmd+` is supposed to open Sublime Text's embedded interpreter console, i.e. the one that you use when developing or debugging Sublime plug-ins. You can verify that by noticing that the sublime module is available.
If you really want Python 3 console there, upgrade to Sublime Text 3 which embeds Python 3.3. Alternatively, use a dedicated plug-in like SublimeREPL (see #MattDMo's answer).
And BTW: If you want a nice environment for interactive Python work, I suggest to disregard the above and give IPython Notebook a shot.
You could try setting the environment variables something similar to the following in Tools > Build System > New Build System
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env":
{
"PYTHONPATH":"/usr/local/lib/python:/usr/local/username/python/3.2/lib/python3.2/"
},//this comma!
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
What you're seeing is the Python that comes bundled with Sublime Text. I wouldn't upgrade it, but if you want to use your own you could do something like:
ln -s $HOME/.pythonbrew/pythons/Python-2.6/lib/python2.6 /Your_Sublime_Install_Path/lib/python2.6

Categories