473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Can I import a file without file extension .py?

Hi all
I wonder if I can import a file with other file extensions ?

Can I do that only with python?

Thank you

Jia Lu

Feb 1 '07 #1
3 2085
"Jia Lu" <Ro*****@gmail.comwrites:
I wonder if I can import a file with other file extensions ?
I use this function in most of my test infrastructures, to import
programs as modules for unit testing.

def make_module_from_file(module_name, file_name):
""" Make a new module object from the code in specified file """

from types import ModuleType
module = ModuleType(module_name)

module_file = open(file_name, 'r')
exec module_file in module.__dict__

return module

--
\ "Men never do evil so completely and cheerfully as when they do |
`\ it from religious conviction." -- Blaise Pascal (1623-1662), |
_o__) Pense'es, #894. |
Ben Finney

Feb 1 '07 #2
>
def make_module_from_file(module_name, file_name):
""" Make a new module object from the code in specified file """

from types import ModuleType
module = ModuleType(module_name)

module_file = open(file_name, 'r')
exec module_file in module.__dict__
Thank you very much.
And can you tell me what does " exec module_file in module.__dict__ "
mean?

Thanx

Feb 1 '07 #3
On Feb 1, 12:51 am, "Jia Lu" <Roka...@gmail.comwrote:
def make_module_from_file(module_name, file_name):
""" Make a new module object from the code in specified file """
from types import ModuleType
module = ModuleType(module_name)
module_file = open(file_name, 'r')
exec module_file in module.__dict__

Thank you very much.
And can you tell me what does " exec module_file in module.__dict__ "
mean?

Thanx
http://docs.python.org/ref/exec.html

It executes whatever code is in module_file and dumps all variables
defined in the process into module.__dict__.

Feb 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bethany Holliday | last post by:
Hi all, I have a file with an extension of .sdf. I "believe" it is a text file of some sort but I am uncertain. The source agency hasn't returned any of my calls so I'm wondering if anyone is...
3
by: ch424 | last post by:
Hi there, I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems extending python in C: The C code is below: #include <Python.h> #include "ni488.h"
2
by: musicloverlch | last post by:
I need to import 60 text files all in one folder. Can I use VBA to cycle through all the files, without having to import them one by one? Does anyone have an example? Thanks in advance. Laura
1
by: sven.nystrom | last post by:
I've re-built an extension module (as a .dll) using the 2.5a1 release. Unexpectedly, I'm not able to simply import it (not the way I can when building it for 2.3). Using imp.load_dynamic() the...
3
by: gdaniels | last post by:
Hi, I need some help. The routine below runs when a command button is clicked. A prompt appears to enter the directory path to the folder containing delimited text files to be imported into an...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
6
by: jao.sanien | last post by:
Hi all I was wondering if it was possible to some how import .DAT files into access form? I have over 500 DAT files which contains infomation about various components and I wouldn't want to...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
4
by: WillMiller | last post by:
Hi, I'm currently attempting to write a small Access Database which carries out the following tasks : 1. Imports Submission files (of a text variety) to a table using TransferText on a daily...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.