473,399 Members | 2,478 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,399 software developers and data experts.

Does __import__ require a module to have a .py suffix?

Hi all. I've got a python file called 'foo' (no extension). I want to
be able to load it as a module, like so:

m = __import__('foo')

However, the interpreter tells me "No module named foo". If I rename
it foo.py, I can indeed import it. Is the extension required? Is there
any way to override that requirement?

Thanks,
--Steve
Mar 12 '08 #1
3 2593
On Mar 12, 11:22 am, mrstephengross <mrstevegr...@gmail.comwrote:
Hi all. I've got a python file called 'foo' (no extension). I want to
be able to load it as a module, like so:

m = __import__('foo')

However, the interpreter tells me "No module named foo". If I rename
it foo.py, I can indeed import it. Is the extension required? Is there
any way to override that requirement?
I think you answered your own question, but if you want more info:

From the Python Tutorial:

http://docs.python.org/tut/node8.html

"A module is a file containing Python definitions and statements. The
file name is the module name with the suffix .py appended."

RD

Thanks,
--Steve
Mar 12 '08 #2
On Mar 12, 12:22 pm, mrstephengross <mrstevegr...@gmail.comwrote:
Hi all. I've got a python file called 'foo' (no extension). I want to
be able to load it as a module, like so:

m = __import__('foo')

However, the interpreter tells me "No module named foo". If I rename
it foo.py, I can indeed import it. Is the extension required? Is there
any way to override that requirement?
You can use execfile:

foo = {}
execfile('foo', foo)

Apart from the different syntax in accessing the module globals
(attributes with __import__ (foo.x) vs dict entries with execfile
(foo['x'])), there are probably more subtle differences but I can't
tell for sure. It would be nice if someone more knowledgeable can
compare and contrast these two appraches.

George
Mar 12 '08 #3
En Wed, 12 Mar 2008 18:02:54 -0200, Jean-Paul Calderone
<ex*****@divmod.comescribió:
On Wed, 12 Mar 2008 12:58:33 -0700 (PDT), George Sakkis
<ge***********@gmail.comwrote:
>On Mar 12, 12:22 pm, mrstephengross <mrstevegr...@gmail.comwrote:
>>Hi all. I've got a python file called 'foo' (no extension). I want to
be able to load it as a module, like so:

m = __import__('foo')
You can use execfile:

foo = {}
execfile('foo', foo)

Apart from the different syntax in accessing the module globals
(attributes with __import__ (foo.x) vs dict entries with execfile
(foo['x'])), there are probably more subtle differences but I can't
tell for sure. It would be nice if someone more knowledgeable can
compare and contrast these two appraches.

Another difference is that when you import a module, its code is
(usually)
only executed once. Each import after the first just returns a reference
to the already-created module object. When you use execfile, the code is
re-evaluated each time.
The steps done by import are outlined in this message
http://groups.google.com/group/comp....a590bca5f2be8c
The relevant part (citing myself):

newmodule = sys.modules[modulename] = ModuleType(modulename)
# constructor sets __name__ and a null __doc__
newmodule.__builtins__ = current builtins
newmodule.__file__ = filename
code = read from filename and compile it
exec code in newmodule.__dict__

Apart from sys.modules and __file__, there is another difference, the
__builtins__ attribute. It is important: if not present, Python executes
the code in "safe mode" where certain operations are disabled (and there
is a big performance penalty).

--
Gabriel Genellina

Mar 19 '08 #4

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

Similar topics

2
by: Marcus Schneider | last post by:
I use PythonWin on WinXP. Every time I change a module, I have to leave PythonWin and re enter to make it notice I have made changes. I guess this is not the normal way to do that.. do I have to...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
5
by: mike | last post by:
regards: Does each HTTP Request have "Referer:" header? Any positive suggestion is welcome. thank you May goodness be with you all
7
by: mike | last post by:
regards: Does each HTTP Request have "Host:" header? Any positive suggestion is welcome. thank you May goodness be with you all
18
by: Steve | last post by:
umm, was just wondering, does the python mascot have a name ? We are naming the conference rooms in our office you see :o). Also, is there a place I could get some neat, good quality pics of the...
3
by: 3c273 | last post by:
Does it signify something? Just curious. Louis
26
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't...
2
by: =?Utf-8?B?RkZD?= | last post by:
Does microsoft outlook 6 have a calender, I cannot find a calender in microsoft outlook 6, is there one? and how do i find it if there is? I there isn't how do I get one? thankyou for your time...
3
MusoFreak200
by: MusoFreak200 | last post by:
hello everyone... i know that i just asked a similar question but... does php require a database or server? thank you for your answers in advance...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.