Sublime Forum

Trying to import 3rd party module

#1

Hello, I’m learning how to develop a plugin for sublime text. I’m a hobist on embedded system and I wanna create a plugin for upload our files to embedded boards which works microPython.

Adafruit created a tool for it, its named adafruit-ampy. ampy uses pySerial library for connect microPython board. So, I want use serial library in my plugin because my plugin have to send files to micropython board with ampy. When I try import serial I get this error

ImportError: No module named 'serial'

I have not any idea how can I import serial library…

0 Likes

#2

You need to download the serial module inside your package folder:

Packages/YourSublimePackage/serial

And perform relative import in your plugin Packages/YourSublimePackage/your_plugin.py:

from . import serial
0 Likes

#3

But this does not allow you to distribute the package with the serial submodule via package control:

  1. https://github.com/wbond/package_control/issues/126 github package with a git submodule dependency?

For that you would need to create a Package Control Dependency:

  1. https://github.com/wbond/package_control/issues/1089 Package dependencies (install dependent sublime packages)
2 Likes

#4

I want import a source from another dependency. Here is my project tree:

bisguzar@heimdall  ~/.config/sublime-text-3/Packages/ampy4sublime  tree  .  
.
├── ampy4sublime.py
└── pkg
    ├── ampy
    │   ├── cli.py
    │   ├── files.py
    │   ├── __init__.py
    │   └── pyboard.py
    └── serial
        ├── __init__.py
        ├── rfc2217.py
        ├── rs485.py
        ├── serialcli.py
        ├── serialjava.py
        ├── serialposix.py
        ├── serialutil.py
        ├── serialwin32.py
        ├── threaded
        ├── tools
        ├── urlhandler
        └── win32.py

I’m importing files.py from ampy4sublime.py (its main file of plugin) and files.py should call pyboard.py. Pyboard.py calls ‘serial’. What can I do?

0 Likes

#5

On ampy4sublime.py you can do:

from .pkg.ampy import files

But you will need to add serial inside ampy as in:

.
├── ampy4sublime.py
└── pkg
    ├── ampy
    │   ├── cli.py
    │   ├── files.py
    │   ├── __init__.py
    │   ├── pyboard.py
    │   ├── serial
    │   │   ├── __init__.py
    │   │   ├── rfc2217.py
    │   │   ├── rs485.py
    │   │   ├── serialcli.py
    │   │   ├── serialjava.py
    │   │   ├── serialposix.py
    │   │   ├── serialutil.py
    │   │   ├── serialwin32.py
    │   │   ├── threaded
    │   │   ├── tools
    │   │   ├── urlhandler
    │   │   └── win32.py

Now you need to edit the pyboard.py to import the serial module using local imports as in:

from . import serial

Instead of:

import serial

Or do not change the folder structure and try to import it as:

from .. import serial
0 Likes