Post 9rkWbZGpimYYPWrJSq by mdhughes@cybre.space
(DIR) More posts by mdhughes@cybre.space
(DIR) Post #9rkTjCvYNR43ZJrB3o by alexbuzzbee@fosstodon.org
2020-02-06T03:43:40Z
0 likes, 0 repeats
Trying to figure out if there's a good way to inline a module into a Python script. I may just have to write my own tool for that.
(DIR) Post #9rkU9KEfpLL9lSFo8W by alexbuzzbee@fosstodon.org
2020-02-06T03:48:23Z
0 likes, 0 repeats
Now thinking about syntax if I do write my own tool. It should probably trigger a syntax error so we don't end up with a missing module.$include file.pyThen a relatively simple script can find all of those lines and replace them with the file contents.
(DIR) Post #9rkUaWZu2w75rX9ZIm by alexbuzzbee@fosstodon.org
2020-02-06T03:53:18Z
0 likes, 0 repeats
Basically I'm trying to statically link Python scripts so I can distribute them without saying "first, install this module I wrote and that you'll only need for these specific scripts..."
(DIR) Post #9rkUoD2FVEedJ0wBDU by pganssle@qoto.org
2020-02-06T03:55:45Z
0 likes, 0 repeats
@alexbuzzbee This is more or less how PyInstaller works, though I will say that you are probably way better off using normal packaging and having people either install your scripts with a system installer or pipx or something.
(DIR) Post #9rkV7IYlBgZtwQFLdI by alexbuzzbee@fosstodon.org
2020-02-06T03:59:14Z
0 likes, 0 repeats
@pganssle PyInstaller bundles an interpreter and the standard library. I'm not looking to do any of that, just to be able to give someone a script and have them be able to run it with an existing Python installation.
(DIR) Post #9rkVRXdmpHIWVRFaCm by pganssle@qoto.org
2020-02-06T04:02:53Z
0 likes, 0 repeats
@alexbuzzbee My understanding is there's a "compile everything into a single file" mode, which is what I was referring to.Though again I sorta think "Here is a wheel install it with pipx then you can run it" or "install this thing in a virtual env and run it" is not that big a deal.
(DIR) Post #9rkVUayCu6vthVG3m4 by mdhughes@cybre.space
2020-02-06T04:03:25Z
0 likes, 0 repeats
@alexbuzzbee You don't need to. Make a directory foo with an empty __init__.py and your foo.py in there with it.from foo import *
(DIR) Post #9rkVk7QqUfRciu3VyK by alexbuzzbee@fosstodon.org
2020-02-06T04:06:14Z
0 likes, 0 repeats
@pganssle My understanding of PyInstaller is that it takes your script, your modules, their dependencies, the standard library, and the Python interpreter, and packs them into a single executable. That's massive overkill for this project. My target userbase already has Python, or can easily install it. I don't want to make someone set up a virtualenv or really install something at all to run a utility script. Including just the module itself into the script is all I need or want to do.
(DIR) Post #9rkVwoh1Tb0SMyg2wy by alexbuzzbee@fosstodon.org
2020-02-06T04:08:32Z
0 likes, 0 repeats
@mdhughes I'm trying to distribute a single-file script. My design parameters do not include an installation phase with any complexity beyond "copy the file and chmod +x it." That excludes unpacking a tar, installing a wheel, or putting something on PYTHONPATH.
(DIR) Post #9rkW15vjKZEGzKb2AK by mdhughes@cybre.space
2020-02-06T04:09:18Z
0 likes, 0 repeats
@alexbuzzbee Unzipping is well within the abilities of most people…
(DIR) Post #9rkWF4IMCrtHyHqKfI by alexbuzzbee@fosstodon.org
2020-02-06T04:11:50Z
0 likes, 0 repeats
@mdhughes What I'm trying to do is distribute a utility that you can just drop in ~/bin with no mess. It's really easier to build the include mechanism. Honestly I probably could have done it by now if I'd not been arguing with people about this for some reason.
(DIR) Post #9rkWbZGpimYYPWrJSq by mdhughes@cybre.space
2020-02-06T04:15:53Z
0 likes, 0 repeats
@alexbuzzbee Well, in that case you don't need anything special. I distribute Filter.py with Utility.py, people just drop them both in bin and "import Utility" works.
(DIR) Post #9rkWyHTG496VrnV7JI by tedu@honk.tedunangst.com
2020-02-06T04:20:00Z
0 likes, 0 repeats
@alexbuzzbee I don't know what tooling is involved, but youtube-dl is an all in one script.
(DIR) Post #9rkeiAa4iMGjUuWBBg by hugot@amsterdon.nl
2020-02-06T05:46:40Z
0 likes, 0 repeats
@alexbuzzbee is this really a good idea? Seems messy to me. Why not use a compiled language in stead?
(DIR) Post #9rkky3yNeMCpMTqfPU by zladuric@mastodon.technology
2020-02-06T06:56:48Z
0 likes, 0 repeats
@alexbuzzbee perhaps a stupid question, but wouldn't pip take care of your deps as I install the script?Not a python person, hence the question.
(DIR) Post #9rkqLucmYeuf5ZwBzE by nitox@fosstodon.org
2020-02-06T07:57:05Z
0 likes, 0 repeats
@alexbuzzbee try this:import py_compile py_compile.compile("yourpythoncode.py")From another python script, it should create a pyc file on the same directory which contains the program compiled into 1 file (i guess)
(DIR) Post #9rlBPad8w5SnH2hXVI by alexbuzzbee@fosstodon.org
2020-02-06T11:53:08Z
0 likes, 0 repeats
@hugot Because Python is a better language for what I'm trying to do than any of the compiler languages I know well.
(DIR) Post #9rlBVIZqyBAl1LsuWW by alexbuzzbee@fosstodon.org
2020-02-06T11:54:10Z
0 likes, 0 repeats
@zladuric It would, but I'm trying to avoid an installation step. Not that people couldn't handle it, but the script in question is pretty trivial and I want to avoid an unreasonably high barrier to entry.
(DIR) Post #9rlBbz3FxqgSUQpEpc by alexbuzzbee@fosstodon.org
2020-02-06T11:55:20Z
0 likes, 0 repeats
@nitox The compiler will generate a bytecode file, but I don't believe it will include code from modules, and I'm trying to keep the script in text form.
(DIR) Post #9rlDhx76HZgI7ob5YO by hugot@amsterdon.nl
2020-02-06T12:18:50Z
0 likes, 0 repeats
@alexbuzzbee I have done similar stuff to this and in my experience a custom solution like yours usually leads to hard to understand, buggy software that others are reluctant to use or contribute to. Maybe it will work out better for you though, could be that I just suck 😛Why do you need to inline modules BTW? And what does your program do? Just curious 🙃
(DIR) Post #9rlE5COdcwvWBUswS0 by alexbuzzbee@fosstodon.org
2020-02-06T12:23:03Z
0 likes, 0 repeats
@hugot The program is a small script that converts data between two formats. It needs a module I'll write to handle one of the formats, but the other is already supported in the standard library.I intend to write several programs like this, thus the format code being a module.I want people to be able to just download the program, run it, and then delete it again without having to go through an install step.
(DIR) Post #9rnPO6D4LB7aJLbn3w by alexbuzzbee@fosstodon.org
2020-02-07T13:33:53Z
0 likes, 0 repeats
@tedu I looked into this and youtube-dl is a Python zipapp, essentially a ZIP file with Python code in it and a Python shebang. Potentially a good way to handle this. Thanks for pointing that out.
(DIR) Post #9rtxKTwMjPty8r0BOq by alexbuzzbee@fosstodon.org
2020-02-10T17:27:44Z
0 likes, 0 repeats
What I ended up doing was building a zipapp, a ZIP archive containing Python scripts. There's a module (zipapp) for building them, and you can use a shebang to make them act like executables.