First time here? Check out the FAQ!
1

Extension script and importing module

I have a script I'd like to bind to a hotkey in wing. The script required the use of psutil, which isn't a part of the standard python library. When wing starts the script errors because "import psutil" isn't valid. I can't figure out where I should install this package. I tried :

AppData\Roaming\Wing Pro 9\scripts

AppData\Roaming\Python\Python310\site-packages

It looks like it can find my dependencies if I put the code in C:\Program Files\Wing Pro 9\bin__os__\win32\runtime-python3.10\Lib, but obviously that can't be the official answer.

Thanks for the help! Nathaniel

nathanieljla's avatar
56
nathanieljla
asked 2023-07-23 10:43:28 -0600
Wingware Support's avatar
4.2k
Wingware Support
updated 2023-12-07 08:24:39 -0600
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

0

One way to approach this is to install your dependencies into Wing's Python using the pip executable located in bin/__os__/win32/runtime-python3.10 -- that's somewhat better than just dropping something into there and would take care of getting a compatible version and other dependency management. Of course this needs to be redone each time you install a new version of Wing from an installer (but not if you just use Check for Updates, since that doesn't replace the base install).

To reduce the chances of somehow breaking Wing, you could also place your dependencies somewhere and modify sys.path to import, ideally removing your added directory after the import(s) are done:

import sys
dirname = '/path/to/dependencies'
sys.path.append(dirname)
import testmod
sys.path.remove(dirname)

Or to avoid modifying sys.path at all, use importlib. This is the cleanest approach in terms of not affecting the rest of Wing's functionality, but also the most complex one to actually implement:

import os
from importlib.util import spec_from_file_location, module_from_spec
dirname = '/path/to/dependencies'
spec = spec_from_file_location('testmod', os.path.join(dirname, 'testmod.py'))
testmod = module_from_spec(spec)
spec.loader.exec_module(testmod)

In practice, I think any of these is fine.

Wingware Support's avatar
4.2k
Wingware Support
answered 2023-07-24 09:39:54 -0600
edit flag offensive 0 remove flag delete link

Comments

Thanks for the reply. Eventually, I'd like to publish this to github for others to use. So I like the idea of pip "get_my_package", however bin/__os__ isn't under a user location, so (if I understand correctly) users who don't have admin privileges wouldn't be able to do this. I'll have to look closer at this next week. You've given me some food for thought. Thanks.

nathanieljla's avatar nathanieljla (2023-07-25 11:36:14 -0600) edit

Pip does have some sort of automatic support for dealing with not having admin rights, but I'm not sure of the details. Another idea is to use the .zip install of Wing, which puts all files into the user space. On Linux, the equivalent is using the .tar installer.

Wingware Support's avatar Wingware Support (2023-07-25 16:18:18 -0600) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer