Python: how to import a python file that doesn't end in .py?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Python: how to import a python file that doesn't end in .py?
Not sure if I'm overcomplicating things, but...
I need to import a python class that's defined in a file that does not end in .py (it has no extension). I'm guessing that the lack of .py extension is why I can't simply add the path and import.
Is there another way to import? Can I read the whole file into a var & exec that var? Is there some other way?
for anyone interested in how to actually solve this problem, here's what I've done:
Code:
mp4_script = 'path/to/python/script'
mp4file = file(mp4_script)
mp4mkr = mp4file.read()
mp4mkr = mp4mkr.replace("__main__","__absolutely_nothing__") # make sure we don't try to run it.
exec(mp4mkr) # now we can use classes in this executable script just as if it were a module
... don't know if this is the "correct" way to importing a non-.py file, but it does the trick.
# mv /path/to/python/script /path/to/python/lib/myscript.py
# python
Python 2.4.2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myscript
or just simply move myscript to the python lib directory without renaming. It will still work.
Last edited by ghostdog74; 04-15-2008 at 01:17 AM.
# mv /path/to/python/script /path/to/python/lib/myscript.py
# python
Python 2.4.2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myscript
or just simply move myscript to the python lib directory without renaming. It will still work.
Thanks for the suggestion, but I don't own the file or directory, don't have write permissions where it lives, can't change the name because I'm not the only one/thing who uses it, and don't have write access to python's libdir.
I could copy it somewhere, yes. I could put a symbolic link somewhere, yes. Solutions like that, however, clutter up the file system with copies of files that could be used otherwise.
ntubski: thanks for execfile. that looks a little cleaner.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.