1

I a trying to run and use a python package on a remote machine without deploying or installing the package.

In the following link it's nicely described how to create a string importer for modules. modules files are read and stored as text in a dictionary. stored in memory and loaded using StringImporter import hook.

I can use the same implementation, read my modules into a dictionary, ship the same dictionary using socket communication and use the same StringImporter on remote machine to import the module.

But I am still struggling to ship a package that looks like the following

my_package
    |-- __init__.py
    |-- module1.py
    |-- sub_package
        |-- __init__.py
        |-- module2.py
        |-- module3.py
Cobry
  • 4,348
  • 8
  • 33
  • 49
  • Glad you found a solution. I'm just curious: why do you need this? :D – AKX Aug 04 '18 at 22:03
  • I deleted my solution because it didn't work the way i expected. I need to run some code on remote machines where permission to write on disk is not allowed. My code is a good big package and shipping it to the machine remote site package is not an option. So i want to transfer the code, keep it in memory and use an import hook that i am having difficulty creating to import the package on the remote machine – Cobry Aug 05 '18 at 01:32
  • Is it an UNIX-like machine? If so, is even writing into `/tmp` allowed? – AKX Aug 05 '18 at 06:20
  • It is a unix machine but still i want to avoid that. Rules ... trust i wouldn't have bothered if i can send my files without trouble. A simple scp would solve my problem. I know python allows importing from a zip file. I tried to create an in memory zipfile of the package to ship using socket normal communication as bytes data but wasn't that easy – Cobry Aug 05 '18 at 06:35

1 Answers1

0

You would need to write your own import class that can use in memory data (tar or zipfile for eg).

See https://docs.python.org/3/library/importlib.html

ideasman42
  • 42,413
  • 44
  • 197
  • 320