LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-14-2008, 05:45 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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
 
Old 04-14-2008, 08:18 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
it not difficult to name it with a .py extension.
 
Old 04-14-2008, 08:36 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 04-14-2008, 08:58 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,442

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
You could use execfile (see http://docs.python.org/lib/built-in-funcs.html)
 
Old 04-15-2008, 01:14 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by BrianK View Post
::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.
 
Old 04-15-2008, 12:35 PM   #6
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ghostdog74 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can't import zlib in python when using a compiled 2.3.5 version laggerific Linux - Software 0 11-15-2006 06:09 PM
Python 'import wx' problem jdwilder Programming 6 07-11-2006 05:17 AM
python, import and execfile() cs-cam Programming 1 10-18-2005 08:47 PM
Python: Idle: ImportError: cannot import name sleep Joe Soap Linux - Newbie 1 10-01-2005 01:52 AM
Python - can I import my own stuff? miknight Programming 1 06-10-2003 07:48 AM


All times are GMT -5. The time now is 02:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration