Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
04-14-2008, 05:45 PM
|
#1
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Rep:
|
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?
Thanks
|
|
|
|
04-14-2008, 08:18 PM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
it not difficult to name it with a .py extension.
|
|
|
|
04-14-2008, 08:36 PM
|
#3
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
it not difficult to name it with a .py extension.
|
::sigh:: really?
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.
|
|
|
|
04-14-2008, 08:58 PM
|
#4
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,018
|
|
|
|
|
04-15-2008, 01:14 AM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by BrianK
::sigh:: really?
|
Code:
# 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.
|
|
|
|
04-15-2008, 12:35 PM
|
#6
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
Code:
# 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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:56 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|