Sublime Forum

Inconsistent errors when importing python modules

#1

Hi,

I am confused about importing python modules in user writing packages. I will try to simplify the problem so that it is easier to track down. My problem is that importing a dependency depends on the location of the python file. When I place the python file below in the Packages/User folder, everything works without a problem. However, when I place the file in Packages/test, I get an error message when importing paramiko (see error below). paramiko is a dependency installed through a different package. Typing “import paramiko” in the console works without errors.

Can anyone help me resolve this problem?

Thanks!

py file

import paramiko
import sublime
import sublime_plugin

class TestCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        print("test")

error message

File "~/Library/Application Support/Sublime Text 3/Packages/test/test.py", line 2, in <module>
    import paramiko
  File "~/Library/Application Support/Sublime Text 3/Packages/paramiko/all/paramiko/__init__.py", line 30, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "~/Library/Application Support/Sublime Text 3/Packages/paramiko/all/paramiko/transport.py", line 34, in <module>
    from paramiko import util
ImportError: cannot import name util
0 Likes

#2

The User package runs in the Python 3.8 plugin host. Other packages need to specify a .python-version file if they want to run in 3.8 and not 3.3. The console runs in 3.8 by default but can be switched in the context menu iirc

0 Likes

#3

I am kinda surprised it works in Packages/User since User runs in 3.8 by default and dependency support is not enabled for 3.8 yet. Whereas, if you do not specify a .python-version file, it runs in 3.3 so dependencies works, which means it should have worked in Packages/Test

0 Likes

#4

Thanks! This is all very useful to know. Adding .python-version works for import paramiko but I am still running into other errors such as the one below.

What further confuses me is that exactly the same setup still works on my old MacBook. In this case, the package is running on 3.3 (without .python-version file) without any of these errors. Now I switched to a new MacBook Pro with M1 Pro chip and it doesn’t work anymore.

New error

from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException

ModuleNotFoundError: No module named ‘paramiko.ssh_exception’

0 Likes

#5

Maybe your M1 doesn’t have openssl installed in /usr/local ?

0 Likes

#6

openssl is installed under /usr/bin/. Not sure how that is related to the “no module error” though.

0 Likes

#7

paramiko.ssh_exception may not be defined if openssl is not installed, but just guessing here :slight_smile:

0 Likes

#8

Thanks for the help. :slight_smile:

0 Likes

#9

Some more details about importing BadHostKeyException etc

from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
  • With Python 3.3, this line works without a problem.
  • With Python 3.8, I get the error ModuleNotFoundError: No module named 'paramiko.ssh_exception'

This seems to be unrelated to intel versus M1 Mac. On my previous Intel Mac, the package ran with 3.3 but changing it to 3.8 throws the same error.

However, with Python 3.3 I get an error for from paramiko import util as detailed above.

0 Likes