11

I know there is a way to import modules which are in a zip file with python. I created kind of custom python package library in a zip file.

I would like to put as well my "task" script in this package, those are using the library. Then, with bash, I would like to call the desired script in the zip file without extracting the zip.

The goal is to have only one zip to move in a specified folder when I want to run my scripts.

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
ForceMagic
  • 6,230
  • 12
  • 66
  • 88

2 Answers2

18

I finally found a way to do this. If I create a zip file, I must create __main__.py at the root of the zip. Thus, it is possible to launch the script inside the main and call if from bash with the following command :

python myArchive.zip

This command will run the __main__.py file! :)

Then I can create .command file to launch the script with proper parameters.

You can also put some code in the __main__.py file to give you more flexibility if you need to pass arguments for example.

ex: python __main__.py buildProject

The reference documentation is here: https://docs.python.org/2/library/runpy.html

serverhorror
  • 779
  • 5
  • 21
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
  • Great. I thought you wanted to arbitrarily call "any" script within your zip archive. – Rod Sep 21 '10 at 18:53
  • Basically it is what I wanted, but without unzipping the file. So it's the only way I found to do so. – ForceMagic Sep 21 '10 at 19:58
  • 3
    I found this [web page](http://www.adaltas.com/en/2018/03/06/execute-python-in-an-oozie-workflow/). You can also run scripts without `__main__.py` like this: `PYTHONPATH=/path/to/myArchive.zip python -m [filename without extension] [args]` – Hyunjun Kim Jul 12 '18 at 02:13
  • 2
    Note that one can run any script in the zip archive, that is, `__main__.py` file doesn't have to be in the root of the zip file: `python /path/to/z.zip/path/within/zip`. This can be used for example to directly run code in a wheel file (which is a zip): `python /path/to/wheel1.whl/module1`. Also, zip files always behave as directories, so one can test this without zip: `python /path/to/any/directory` would run `__main__.py` inside a given directory. – Ivan Aug 17 '19 at 12:19
  • However, you can't run any file that isn't a `__main__.py` file. – DrownedSuccess Nov 09 '22 at 20:55
3

Have a look at zipimport. It should work directly from the install. You might have to do some work to make the pythonpath point to your zip file/directory.

This module adds the ability to import Python modules (*.py, *.py[co]) and packages from ZIP-format archives. It is usually not needed to use the zipimport module explicitly; it is automatically used by the built-in import mechanism for sys.path items that are paths to ZIP archives.that are paths to ZIP archives.

Rod
  • 52,748
  • 3
  • 38
  • 55
  • What I meant was to run python script inside the zip with bash command in order to create a .command file that is launch the desired script. Maybe it's not possible without python? – ForceMagic Sep 21 '10 at 14:34
  • I am not aware of anything that could call a script directly inside a zip archive. Sadly, you have to rely on python to read the python scripts which prevents you from using the __main__ == "__main__" idiom. – Rod Sep 21 '10 at 15:11
  • Ok, thanks a lot! Instead, I'll try to find something to create a application like with my python files. Because in the current situation, I need to have only one file that my users can click and the script run with its modules. I was thinking about py2app, because I'm on Mac. – ForceMagic Sep 21 '10 at 15:20
  • The best link I found for building app with python script : http://stackoverflow.com/questions/2933/an-executable-python-app – ForceMagic Sep 21 '10 at 15:42
  • 1
    Contra @Rod, above, @HyunjunKim's comment on @ForceMagic's answer, which deserves to be an answer in its own right, worked just fine for me with a script, one of many in the zip, which uses the `__name__ == "__main__"` idiom. – Martin Dorey Sep 26 '18 at 23:14
  • @MartinDorey Feel free to create it and put both element of response in there. At the time, I believe only the Rob answer wasn't enough or working out of the box for me. – ForceMagic Feb 16 '23 at 21:35