Sublime Forum

Queston about import sublime and python3

#1

when I run in python3.6

import sublime
import sublime_plugin

it fails with no module named sublime. My code is python3 as python2 is dead but it seems to be being run as python2 inside sublimes inner workings beyond using a New Build System set as python3 which I have setup. I think that the build system only effects the the code interpreter used to run the users code not Sublime Package code but im guessing because really how would I know im just a monkey?

So I went looking and there are a few sublime packages in pip3 search sublime that I could install sublime-cli, sublime-python, sublime_harness including venv’s I never knew existed, pretty cool but I dont think will really solve my problem as if others were to use the plugin they would have to pip3 install sublime-whatever* as well? I don’t know which package to install even to test.

I also tried to find the Example Plugins in $USER/.config.sublime-text-3/Packages/Default but the folder doesn’t even exist and I honestly cant remember deleting it. Maybe it’s in a different Packages folder the API page is pretty ambiguous but I understand the necessity being a cross platform program. But I cant find any examples to draw inspiration let alone a fix.

I did notice a single package in pip3 search that seems like it could be a python3 version of sublime text called sublime-text but pip is lacking on it’s available package information so it could establish a connection to an ant farm on a small planet of Proxima Centauri C. Pip’s allot like playing darts after you’ve stabbed your eyeballs out with a rusty fork yet I’m determined to figure out how to get a bullseye. I really don’t feel like randomly installing and uninstalling pip packages though.

Any way the one thing I know for sure is that I’m not certain about any of this.

0 Likes

#2

These are modules that are part of Sublime Text itself, and represent the API used by Sublime Text plugins and how they interface with the core of the application. As such, non-plugin scripts can’t import them.

These are most likely stub modules that mimic the Sublime API to allow for things like external test services to load and test Sublime plugins from outside of Sublime text.

So, your plugin users would only need to install these if for some reason they wanted to execute tests on your plugin themselves. In such a case they’re most likely already developers, so this isn’t that big of a deal.

Packages that ship with Sublime (such as the Default package) are stored as sublime-package files in a special directory in the Sublime installation folder and not in the Packages folder. That’s to stop you from trying to modify one, which is a decidedly bad idea.

The View Package File command from the command palette will allow you to see (in a read-only view) the contents of any file contained in any package, so you’d use that if you want to peek at the default plugins mentioned in the API docs to see what they’re doing.

All that said, it’s probably important to also mention that writing plugins for Sublime isn’t done by writing Python code and executing it in an external Python interpreter. Sublime has it’s own embedded copy of Python that’s distinct from any version you may or may not have installed on your system, and it uses that. The sublime and sublime_plugin modules are available to plugins via that plugin host without you having to do or install anything else.

If you’re just getting your feet wet with plugin development, you probably want to start by creating simple plugins in your User package. Python files in the top level of any package (including User) are considered plugins by Sublime and will be automatically loaded at startup and reloaded every time they change, so you don’t have to do anything special to get them to run other than saving the file.

I’m currently running a Plugin 101 series on my YouTube channel that’s teaching package and plugin development in Sublime that you may find useful in getting up to speed with things. It assumes that you’re familiar with using Sublime and know how to write Python code, but otherwise teaches how to create plugins.

0 Likes

#3

Thanks for the location of the “Default” folder. Now I can at least have a peak at the files they said to look at for reference.

So as far as the no module named sublime, that would lead me to believe that python3 code cannot be used to design packages because the API is only designed to use python2? Or does sublime respect shebangs to tell sublimes package interpreter which python version to use?

My code works flawlessly everywhere but inside of sublime’s Package Interpreter and any version of python before #3. What I dont know is how to get sublimes package code interpreter to use python’s current language of 3 and import sublime that it doesn’t have?

Maybe I just cant do what I want yet. I can understand that. There has to be thousands of plugins written in 2.7 and upgrading to python3+ might break most of them, so I’m patient.

I have been editing the same ColorPicker package code for three years to get rid of those horrible tool-tips in GTK that block usability(was going to fix wxPy as well). This is just the first time I tried to expand on his functionality and share it to help the community after reading the github page bugs and trying to address a few issues I saw that I knew I could help with like accepting RGB color tuple and a fallback to see if the clipboard has a color already stored and using that for the opening color. I really could rewrite the less than 20 lines of code in python2 by using a different available re method but I refuse to go backwards and write code that’s obsolete. I would rather wait for the world to catch up than go backwards.

Regardless Im up for a good learning experience. I’ll check out your 101 class.

0 Likes

#4

Perhaps you can describe what exactly it is that you’re trying to do?

The plugin host in ST3 runs Python 3.3.6, so yes, every plugin that’s currently available runs in at least that version of Python; only Sublime Text 2 (and likely Sublime Text 1) uses Python 2.

Sublime doesn’t respect the shebang in a plugin because Sublime doesn’t hand off plugin code to any Python interpreter but it’s own plugin host, which is not open to any sort of negotiation (except in ST4, where you can optionally have it run in the newer plugin host that runs Python 3.8 instead of 3.3).

If you’re doing anything with Python and a Sublime Plugin and import sublime doesn’t work, then you’re not writing a plugin; you’re writing a Python script of some sort for execution outside of the Sublime Text plugin host.

0 Likes

#5

I have downloaded the ColorPicker app by weslly. Been using, modding and loving it for years. In the sublimecp.py file I have added some regex as

from re import (fullmatch as re_fullmatch,
                        split as re_split,
                        compile as re_compile)

if I run the program with [ctrl+b] I get no module named sublime because it’s being run in python3 and I guess without sublime available as a package which makes sense now that you’ve explained that the sublime package doesn’t exist outside sublime, so If I execute the program with the built in command of [Ctrl]+[Shift]+C I get nothing so Im assuming it is being run in python2 and freaking out because it cant find re.fullmatch which is at the very head of the file and should not exist in 2.7. I think it’s a pretty solid educated guess? I don’t know how to get the errors anyway. Probably in some syslog? Maybe starting sublime with a flag from the terminal? This info seems to be pretty much ghost town although probably covered in your 101 course that I haven’t watched yet.

If I put all my code together and it runs perfect in python3 by itself. I have GTK.ColorSelectionDialog accepting rgb tuples outside of sublime when run from any terminal. But no matter what I do when I run it in sublime via [Ctrl]+[Shift]+C it never works to varying degrees including no process spawned, process running no window must be killed with kill -9, to complete computer lockup which only happened once after executing the script multiple times while using the original codes outdated GTK import methods with some added code to get the clipboard info using GTK.Clipboard(), something bad happened there but we learn we move on.

So basically what I was trying to do is

__regex_tests = {'hex': re_compile(r"^#[A-z0-9]{6}$"),
                 'is_rgb': re_compile(r"^\([0-9]{1,3},\s?[0-9]{1,3},\s?[0-9]{1,3}\)$"),
                 'get_rgb': re_compile(r"^\(([0-9]{1,3}),\s?([0-9]{1,3}),\s?([0-9]{1,3})\)$")}

def get_clipboard():
    tStr = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD).wait_for_text()

    for i, rgx_test in enumerate(list(__regex_tests.values())[:-1]):
        if re_fullmatch(rgx_test, tStr) is not None:
            if i == 1:
                ## RGB
                return rgb_to_hex((int(x) for x in re_split(__regex_tests['get_rgb'], tStr) if x != ''))
            else:
                ## Hexadecimal
                return tStr
    return None

def rgb_to_hex(rgb):
    hexColor = ''
    for color in rgb:
        hexColor += '%02x' % color
    return '#' + hexColor

get_clipboard() tests what is in the clipboard for hexadecimal or rgb tuple and if rgb tuple converts it to hex I was planning on setting a flag before the GUI colorPicker is executed storing the color format so it can be used to convert the return hex color code to RGB tuple matching it’s input color format. Originally I coded this for the linux python file that handles the GUI but I was going to modify it so that it could be used for all the OS’s by modifying the plugin that calls the GUI scripts and test it’s input before the GUI’s are even called and use testing the clipboard if the original input is ‘’ or None, giving all that functionality to all users not just me in the linux kingdom.

Does that make any sense?

0 Likes

#6

This file isn’t a Python program, it’s a plugin. As such, you shouldn’t be running it via Ctrl+B because it’s not meant to be executed that way (hence the error you’re seeing).

When you press the key Ctrl+Shift+C, you’re telling Sublime to invoke the color_pick command. From looking at the source, that command should always be doing something.

This information is available in the Sublime console, which you can get to via the View > Show Console command. Along with status messages from Sublime, the Python interpreter built into the plugin host also displays it’s output here.

I would check there to see if there’s a syntax error in the file that you edited that’s stopping it from compiling, or a runtime error that’s stopping the command from executing.

0 Likes

#7

I get how this works, used sublime for years and I can read and figure out what code is doing.

Now we are talking! I get what is now happening. It’s re.fullmatch but not because sublime running python2.7 like I thought. I just looked at the re module and re.fullmatch is not available till python3.4. and you say that sublime is up to 3.3.6.

I knew it had to be the re.fullmatch because it was at the head of the file and it was literally the only thing it could be.

So now all I have to do is wait for sublime to upgrade. I’ll check out your 101 after I complete the project that I paused to hack this little sublime/GUI fix together.

Thanks, OdatNurd!

0 Likes