3

I would like to import a class, lets call it MyClass, and assume it is stored in a file MyClass.py. However, the class itself depends on various files distributed over several folders. So if I want to import that class in a Python session, I would write

import sys
sys.path.append('/path_to_folder1/')
sys.path.append('/path_to_folder2/')
sys.path.append('/path_to_folder2/')
from MyClass import MyClass

This works fine, because all file dependencies are found in those folders. Now, I would like to run MyClass on Ipyparallel, i.e. I want to do the following

import ipyparallel
clients = ipyparallel.Client()
dview = clients.direct_view()
with dview.sync_imports():
    from MyClass import MyClass

However, this yields an error, saying that the module MyClass cannot be found. This is because the sys.path.append is only added locally, and not to the different instances of python created by ipyparallel. But so my question is then, how can I import MyClass? I have found a partial answer here Import custom modules on IPython.parallel engines with sync_imports(), basically saying that when running ipcluster from the path where MyClass.py is stored, it will automatically find MyClass. However, I will then still obtain an error, since MyClass itself has dependencies in the various folders. So my question is then: how can I add the additional folder dependencies (e.g. with PYTHONPATH) such that I can import MyClass with sync_imports?

user56643
  • 363
  • 1
  • 3
  • 12

0 Answers0