For example, I have a few python scripts, that link together and some libraries for it that need to be imported. I’m trying to reduce it to only one script.
So instead of:
import library.py
Can I just take the coding from library.py and put into the main script?
There are no limits to the size of a
.pyfile, so you can certainly perform the copy and paste (and edit) operations you have in mind.Some caveats:
is unlikely to do what you want: it imports a module named
pyfrom a package namedlibrary. Thus it requires the existence of a directorylibrarywith an__init__.pyand apy.pyin it (or.pyc, etc). I suspect you meanimport library.Merging all the needed modules into one large
.pyfile only works for modules for which you have Python sources (.py) — you need different techniques if all you have for a module is bytecode.pycor.pyo, and if any of the modules are native-code binary Python extensions (.pydor.soor.dylibetc etc, depending on your system) that just won’t work.You may have to do some renaming, e.g. when you import two modules that both define name
fooyou’ll have to alter one or both of those namesfoo. That’s because, via imports, you get qualified names (so there’s no problem ifa.fooandb.fooboth exist), but without imports you get bare names so you’ll need to disambiguate them “manually”.