How to use the console in Sublime Text 3 - sublimetext3

There is Hide/Show console option in "View" menu of Sublime Text 3. I tried shell, Python and Ruby commands in it but it always returns an error.
What is this console used for?

The Unofficial Documentation (which I prefer to the official documentation) states in the "Basic Concepts" section:
Sublime Text exposes its internals via an Application Programming Interface (API) that programmers can interact with using the Python programming language.
It uses an internal Python interpreter which is not the one in your system's PYTHONPATH variable. So, basic Python commands should be possible for you as it is working for me. The console output is:
...
skipping some console output before
...
plugins-loaded
>>> x = 1
>>> y = 2
>>> f = x * y
>>> print(f)
2
The purpose of the console is to interact with an API to control Sublime Text 3 internal settings:
>>> sublime.version()
'3211'
or:
>>> sublime.message_dialog("test")
which opens a clickable dialog with the text "test".
See the API reference for more information.

The console in Sublime is a window into the internal workings of Sublime in order to see the state of plugins and interact with the plugin host; the most important distinction to make is that the Sublime console IS NOT a Terminal; if you want access to arbitrary commands, you need to install a package such as Terminus to achieve that.
Most of the functionality in Sublime (including default functionality out of the box) is defined in Python plugins, and third party packages can also define Python plugins as well. The console is a window into that Python environment; if a plugin fails (like if the code is broken in some fashion), then the error that the code is generating ends up in the console.
Sublime also displays its own status into the console as well; the console contains output that indicates when plugins are being loaded and unloaded, how long it took Sublime to start up, what version you're running, etc. Any time you see an error dialog pop up, there's also a message added to the console (and in some cases errors go only to the console without being visible elsewhere).
Besides the major distinction that the console isn't a Terminal, it's also important to note that although you can enter arbitrary Python into the console and have it run, it's not meant to be your gateway to running a Python program (unless you want to evaluate single expressions and the like).
Many people fall into the trap of thinking that because Sublime uses Python for plugins that you can also use that environment to run your Python programs as well, but this is not the case. The Python environment that Sublime provides is for its own use, and is distinct from any version of Python you may or may not have installed on your computer.

Related

Inconsistent Imagesnap behavior taking pictures on MacOS X, depending on calling environment

EDITED POST
When I first wrote this, I was mystified by the varied behaviors of the "imagesnap" USB cam image capture program on MacOS. Sometimes it took pictures and sometimes it didn't, and this seemed to vary with the environment where it was called: such as directly in a Terminal window, in a shell program, in a Python program running in the Terminal, in a Python program running in the Pycharm IDE, called with os.popen(), subprocess.run(), subprocess.call(), subprocess.popen(), etc.
Sometimes imagesnap would work just fine and take images, and sometimes it would fail silently. I was really having a hard time figuring out why the behaviors were varied, and how to get picture taking using imagesnap on my mac to work reliably when called from a Python program.
I searched on keywords like MacOS, Catalina, imagesnap, USB camera, webcam, Python, PyCharm IDE, shell command, os.popen(), subprocess.run(), subprocess.call(), subprocess.Popen(), and more. I did not find the solution anywhere, and I didn't get an answer here.
I've finally understood the problem better, and that's why I've rewritten this question.
What's behind the inconsistent behavior of imagesnap when called from different environments, and how can I call it from a Linux program and get it to reliably take pictures?
I now understand that MacOS X's privacy settings in System Preferences control which environments are allowed or silently denied to access USB cameras.
For more detail, and a workaround, see my own answer below. I hope that others who understand the issues even more will add improved answers.
ORIGINAL POST:
Below is a simple Python test program. It uses os.popen() and
captures an image from a selected USB webcam using imagesnap -d and
saves an image in image.jpg.
But the imagesnap -d command behaves differently when running in the
PyCharm environment, returning a blank response (' ') and failing to save the requested image.
Submitting this same code WITHIN PyCharm's terminal window fails in
exactly the same way.
But when the same python commands are submitted to the terminal
outside Pycharm, the program takes the image successfully and reports back as shown in the output below.
I'm running Mac OS X Catalina, and using PyCharm 2019.2.3
Community Edition for development. Python version is 3.7.4
Sample code:
import os
return_string = os.popen("imagesnap -d 'DEVICE_NAME' 'image.jpg'").read()
print("'",return_string,"'")
Within the PyCharm development environment this program returns the following and does not create the jpg file:
/Users/mcgregor94086/PycharmProjects/SonaScannerGUI/venv/bin/python
/Users/mcgregor94086/Library/Preferences/PyCharmCE2019.2/scratches/scratch_5.py
' '
Process finished with exit code 0
Outside PyCharm this same code returns:
$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> return_string = os.popen("imagesnap -d 'DEVICE_NAME' 'image.jpg'").read()
>>> print("'",return_string,"'")
' Capturing image from device "<AVCaptureDALDevice: 0x7fa9bc203330 [DEVICE_NAME][0x141717501bcf0b09]>"... '
>>>
After digging into this problem a great deal, and without any responses, I started to search the web more broadly for answers. And although I found almost no information on the web to guide me what was happening, I now believe that I know what is preventing me from taking pictures programmatically within a python program. I also believe that I now have a reasonable hypothesis as to why I am seeing the results I was seeing.
I have a work around that is currently working, but which I fear may not in the future.
The believe the problem has to do with MacOS and I believe it may vary with the specific release of MacOS. I am currently using MacOS X 10.15.1 Catalina. earlier MacOS X releases might not be as strict.
In the Catalina release at a minimum, ANY connected USB Camera is considered a special device governed by special security, and management of access to the camera device is controlled in the Systems Preferences -> Security & Privacy panel.
When you click the Camera row in the menu on the left of the panel you will see a checkbox list of apps which are allowed to programmatically access you camera. If you unlock the page (lower left), you can change which apps are allowed to access the camera by checking or unchecking them in the list.
On the machines I have available, there is a list of 4 apps in this panel: Terminal, UberConference, Google Chrome, Skype.
The list of available apps varies for other resources, such as Contacts, Calendars, Accessibility, Full Disk Access, etc.
Some of these resources (such as Accessibility and Full Disk Access) selection lists have a pair of "+|-" buttons below the app list that allow you to add, or permanently remove, apps from accessing that resource.
But Camera. Microphone and some other resource do not have such a pair of buttons, and for these devices, the list appear unchangeable by the user.
The panel suggests that the apps get on the list by "requesting access" to the camera, but I have not found an explanation anywhere on line about how to add an app to this access list.
One of the predefined apps in the list is "Terminal". That was checked, so when I ran imagesnap from within a terminal window the program had the necessary privacy privileges and the camera took pictures just fine.
In contrast, when I tried to run a python program within the PyCharm IDE, the PyCharm IDE did not identify itself as a terminal and the request silently failed.
Python has a number of different ways to launch an independent program such as imagesnap. These methods include os.popen(), subprocess.run() and subprocess.Popen(). Some of these methods are deprecated, so we can't rely on them working in the future. Other methods() have additional options which control whether a new "shell" is created or not, and these may complicate determining what environment the app will see.
I found it confusing to try to predict which of these python methods
would succeed in opening the camera and which would not, as well as
their behavior when run inside the IDE vs. in a terminal window.
Perhaps someone else can compile such a table.
Work around: For the present, I was able to work around this problem by using os.popen, by having it open a new terminal with an initial string. This seems to ensure that Apple security sees the parent as the "Terminal" app which we specifically authorized in the control panel.
I then found
os.popen("open -a Terminal.app imagesnap.sh")
worked.
While this work around is working for me at present, I was not happy about using os.popen() instead of one of the subprocess methods, as I worry it may be deprecated. Second, this worked with a shell script ("imagesnap.sh") but I have not figured out how to pass parameters or run executables.
Perhaps a knowledgable reader can help with better suggestions.

Is there a way to trigger a "folder open" from Sublime Text 3 API?

I working on a plugin which will generate some file and I need to prompt the user somehow so that he selects a folder where these files will be generated.
Is there a way to do this with Sublime Text 3 API?
Probably the most straightforward way would be to open an input box (sublime.show_input_panel()) with a default value and allow the user to enter their own path if desired.
If you were using the standard Python 3 distribution from python.org, you could always use something like tkinter.tix.DirSelectDialog() or tkinter.filedialog, but the stripped-down version of Python 3.3.3 shipped with Sublime Text 3 does not include several modules, including tkinter. If a graphical interface is important to you, you may want to consider including a small GUI toolkit like easygui with your plugin.

Python built-ins not recognized

I'm using the Redhawk IDE. I noticed when I write Python in the IDE, none of the built-ins (len(),str(),True,False,etc...) are recognized, which gives errors and causes them to be underlined in red.
However, if I install to Target SDR anyway, the component seems to function normally.
How can I get Python in the IDE to recognize built-ins so I can get rid of the faulty errors?
Erik's answer led to the solution. What worked for me is:
Window -> Preferences
PyDev -> Interpreter - Python
Remove Python interpreter
Click "New..."
Enter interpreter name ("Python" works) and select path to interpreter
By default, the top option was selected. Make sure it isn't:
Click "OK"
Click "Apply"
You should now see that the built-in functions are recognized by Python.
If you can't run any Python in the IDE, there are two possible explanations:
1. You have an incorrect Python path being set for you by Eclipse, and you will have to adjust it manually. Using this article from Opal, you can easily check that Python is in fact correctly linking to your IDE, and if it isn't you can point it in the right direction.
2. You have a broken installation of Redhawk. Sounds dubious but you can always try a fresh install.

Python Launcher is Editor?

So I've been playing around with IDLE. Then the Lesson2 tells me to open the editor window, not the shell window. I'm not sure which is Editor? I have EDLE, Python Launcher (downloaded from python.org) and TextWranger...maybe I misunderstood about sth? :'(
IDLE combines several functionalities. It contains an interactive interpreter (the window where the >>> appears in, and in which you can bring code to execution immediately), and it's a small-scale IDE (integrated development environment), which means you can load, edit and save python-files, and launch them conveniently. This functionality is meant with "Editor". Probably just go to the Menu and pick something like "New File".

How to run Python in komodo

I am completely new to Python and wanted to try this code from the tutorial:
istrue = 1
if istrue:
print ("be carefull!")
The code itself should be fine, but I can not find any way to execute this code inside the editor [Komodo-Edit](http://www.activestate.com/komodo-edit)
I am used to Visual Studio and QtCreator (experienced C++/Qt developer). I would expect a menu for debugging and a command such as 'start debugging' which should open a console or use a console inside the editor. I would in any case refuse to use a dos console because then the whole idea of using an IDE would be useless.
If other Python IDEs would be more useful (on windows, no costs) I could switch to another one (except for vim/emacs).
I used to use Komodo edit, but not anymore as It's more of a text editor than an IDE. I reccomend using Ninja-IDE or Eclipse with PyDev. But if you insist on using Konodo Edit, here's an option:
Go here: Toolbox > Add > New Command...
in the top field enter the name 'Run Python file' or something else. Then go to the 'command' field and enter this:
%(python) "%F"
Optionally, you could also specify key binding for fast python executing.
I tried Komodo Edit version 9 and 10. Matthias' method work for the version 9. Version 10, I couldn't see the option to choose the Interpreter.

Resources