Importing OpenCV and Numpy

Hi! I trying to add toolkit to standalone publisher which will upload image sequence converted to mov automaticly. To achive this i used OpenCV and numpy packages, and here problem is starting, i cannot find any way to sucessfull import any of those packages into shotgun, maybe somebody did it before?

3 Likes

Hi,

It is not really clear what you mean, but if i understand correctly you are trying to import those packages in a toolkit app run from Shotgun Desktop, right?

Bear in mind I’m primarily dealing with windows platform, but Desktop comes with a fully functional python interpreter all together with pip. I wouldn’t suggest you install to site-packages but pip provides this useful flag -t (–target) which allows you to install packages to a location of your choosing. This can be used to install OpenCV (numpy will come along as it is a dependency) to network location which you can add to PYTHONPATH before starting Shotgun Desktop (tip: use a hook such as bootstrap.py to manipulate PYTHONPATH from your config).

That way you’ll be able to use both those packages in your toolkit app.

Of course, the location where packages are will have to be accessible to all parties that will use your app. Technically, you could distribute them with your config as well, i guess, but those are pretty heavy afaik.

There are other ways, this is just the simplest one i know.

Cheers,
Aleksandar

5 Likes

Thank you!

Your advices are very helpful. For now I have copy bundle_init.py to config/core/hooks location and modiffied it by adding this code:

pckgs = ('here put tuple of paths to packages you will import')
for path in pckgs:
            if path not in os.sys.path:
                os.sys.path.append(path)

To execute method, and it work well, but i must test it little more. Also I tried to modify bootstrap.py file in config\core\hook folder but it dosent work

2 Likes

Glad it worked for you.

There are, of course, multiple points where you can “inject” stuff like this and different cases will work best in different scenarios. To be honest, I don’t even remember what i did in that regard (documenting is important, tho :wink: ).

The reason i suggested bootstrap.py is simply because this hook is executed before everything else. bundle_init.py executes after the bundle (engine, framework or app) is initialized. Not a problem for you but I’ve had situations where package is needed during the initialization and in that case i need to add it to sys.path earlier.

There is a hook in tk-multi-launchapp called before_app_launch.py which you may expect would be perfect until you realize toolkit apps do not go through tk-multi-launchapp, etc.

Well, in any case…

Cheers,
Aleksandar

2 Likes

One approach that’s worked well for us in the past is to pip install all the needed python modules to a relocatable virtualenv (https://docs.python-guide.org/dev/virtualenvs/) and copy it into a custom sgtk framework that activates the environment on load, making it accessible to any sgtk spps. It has the added benefit of keeping the code somewhat self-contained and portable if it every needs to be used outside a standard production environment (i.e. deployed remotely).

2 Likes

Thanks, all those info are priceless for me :slight_smile: . At this moment I try find out purpose of all those files and making own hooks.

1 Like

I was thinking about something like this, must try in near future :slight_smile:

1 Like