-2

I'm writing a set of Python modules that are essentially utility modules for other code that is contained in dynamic libraries. To make packaging and use easier, what I'd like to do is bundle them inside the library (on Windows as a string resource, on Linux I'm not sure yet - probably export a function that returns the string). Now I'm wondering if there is a way to import a Python module as a string literal of its source code. So essentially the equivalent of

mymod = "def func():\n    return 1"
import(mymod)

Any imports in the imported module itself should also work. Ideally I'm thinking of some way of providing a callback function that is passed the name of a module and returns a string with its contents, so that I can recursively have my modules be loaded; but as a backup I can also live with the situation where upon doing the import of the main module, I would do the same thing in the init in python (i.e. dynamically load any dependencies manually - of course I don't like doing things manually, hence why this is a fallback :) )

Oh and I'd like this to work in Python 2.7 and 3, if that makes a difference...

Roel
  • 19,338
  • 6
  • 61
  • 90
  • 2
    Any reason why you’re not using the standard Python way for packaging dependencies, which is well-established, standardised, easy, and has tons of tool support? – Konrad Rudolph Apr 23 '18 at 14:03
  • have you seen [this question?](https://stackoverflow.com/q/5362771/6779307) I'd be skeptical that this is really the simplest solution (or even a practical one). I'd encourage you to write a question that describes more fully the problem you're facing. There is very likely a more stable and easy to deal with solution. – Patrick Haugh Apr 23 '18 at 14:04
  • This is basically `exec`, but it's a terrible idea for the described use-case and I'll downvote anyone that posts it as answer. What you really want is to read the [PyPA packaging guide](https://packaging.python.org/). – wim Apr 23 '18 at 14:06
  • Because this is for a niche product in which the Python stuff is tangential to the main product; and needs to interface with a Python installation that is provided by yet another product, which I don't want to interfere with. So I can't make public packages for this, nor install a package server myself. Also versioning - it's much easier to just compile whatever is 'current' into the library and I know it'll work, instead of having to sync two projects. Lastly users of these packages only do so indirectly, they generally wouldn't even know what Python is (apart from the slithering variety). – Roel Apr 23 '18 at 14:10
  • That doesn't matter, if you have a Python installation then you have (or can easily create) a site-packages directory in `sys.path`. It is trivial to install/uninstall packages even without any network access (you can even use `pip` if you wanted). You do need packaging, but you don't need a package server and you don't need "public" packages. – wim Apr 23 '18 at 14:13

1 Answers1

-1

If you are looking for a way to execute code from a string (which is not recommended, you might be better off going through the whole process of setup.py to make your library installable) you can use the exec statement as follows

exec(mymod)

This will parse the string as it would normal Python source and execute it, leaving you with it's side effects (such as defining functions and variables). This will work in both Python 2.7 and 3.x. See the documentation here and here for more details.

Alternatively, in Python 2.7 only, execfile does the same thing as exec but for a text file

execfile("path/to/my/mod")

The documentation explains what it does and doesn't do.

T. Laferriere
  • 77
  • 1
  • 6