Sublime Forum

Trouble importing perforce API in package

#1

Hello! Years ago I wrote a ST package that adds perforce support and I’m trying to get it up and running at a new workplace. (I’m aware there’s another existing perforce package available via Package Manager.) I’m in ST build 4169 on linux.

The perforce python API is implemented in two files, P4.py and P4API.cpython-39-x86_64-linux-gnu.so. The P4 module itself does an import P4API internally.

At the top of my package I have from P4 import P4 and a print (sys.path) which produces this output in the ST console:

reloading python 3.3 plugin PerforcePlus.PerforcePlus
reloading plugin ... etc ...
PATH: ['/opt/sublime_text/Lib/python3.3.zip', '/opt/sublime_text/Lib/python33', '/opt/sublime_text/Lib/python3', '/home/megatron/.config/sublime-text/Lib/python33', '/opt/sublime_text/Packages', '/home/megatron/.config/sublime-text/Packages']

So I tried putting those two files in the home dir .config python33 path. My import P4 succeeds, but the module’s own import P4API fails:

File "/home/megatron/.config/sublime-text/Packages/PerforcePlus/PerforcePlus.py", line 17, in <module>
    from P4 import P4, P4Exception
  File "/home/megatron/.config/sublime-text/Lib/python33/P4.py", line 420, in <module>
    import P4API
ImportError: No module named 'P4API'

Anybody know how I might fix this? I don’t know anything about how python uses .so files, but simply installing them next to each other works just fine in regular old python in the shell.

Incidentally I also tried appending to sys.path rather than copying the module somewhere in the ST path and had the same problem: the P4 import succeeds but the P4API import fails.

(Side question: when I print sys.path in the ST console, it shows python38 paths instead of 33. Why is that? Also why does ST appear to load my package as 3.3? Don’t know if that’s related to my problem, just curious.)

0 Likes

#2

By default, the console is py38. You can switch by right clicking the console input area.

To use py38 for your plugin, create a .python-version file whose content is 3.8. E.g., https://github.com/sublimelsp/LSP-pyright/blob/9f7b9a3fda6ce19a69f4d51cf51fcdee4feea6f8/.python-version

Related official docs: https://www.sublimetext.com/docs/api_environments.html#python-version


P4API.cpython-39-x86_64-linux-gnu.so

This is for py39 however.

0 Likes

#3

Thanks so much for the docs link. I’ve added that file and I see the paths switching to 3.8.

Is it likely that the 3.8 python vs. 3.9 .so is the source of my problem? I’ll see if I can track down a 3.8 perforce build.

0 Likes

#4

I found a 3.8 binary and that indeed did solve my problem. Thanks again for your help.

2 Likes