473,387 Members | 1,624 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,387 software developers and data experts.

Import function/module; with no name (ModuleWithNoName ;)

Hi!

I'm somewhat new to python (using 2.5 on w2k)

Currently I am trying to organize a structure for a project, and import all necessary files and so on.

Everything went fine until I tried to import a python file with no name (don't ask me why it has no name ;)

e.g.
root (the folder where the code is run from)
root/.py (the file)

Now, how can I load that file as a module? It doesn't have a name...

I have tried manually loading the file (using open()) but how do I "read" the content so that it is loaded as a module and assign it to a variable (which will work as the module name)?

e.g.

Expand|Select|Wrap|Line Numbers
  1. f=open('.py')
  2. import whateverThatCanDoThis
  3. ModuleName=whateverThatCanDoThis.loadModule(f)
(I've tried playing with something called ihooks but it requires a file name)

I need to know if this is possible? (any suggestions for a solution is also welcome ;)
Jul 12 '08 #1
5 1514
tharden3
916 512MB
ooh, that sounds tricky... I donno. I've never tried to import something with no name
Jul 13 '08 #2
Laharl
849 Expert 512MB
Any chance you can rename the file to something sane? That'd be the simplest solution here...
Jul 13 '08 #3
tharden3
916 512MB
Isn't it a bit ironic that your profile name on this website is ManWithNoName?
Jul 13 '08 #4
Thanks for your replies!

tharden3
>"Isn't it a bit ironic that your profile name on this website is ManWithNoName?"

Yeah, and it's biting pretty hard ;)

Laharl
>"Any chance you can rename the file to something sane? That'd be the simplest solution here..."

I know... Preferably I would not want to name the file, but if python leaves me with no choice...

Also, this will cause problems on other fronts too, I have files in different directories with the same filename, how do I load them in the main code when their filenames will be the same?

I would like to learn how to do this "manually" so I can have full freedom when importing files.

The only solution I can think of is that I load the file and "eval" it. That should be possible, right? But how does that effect the code? (performance wise), is it recommended?
Jul 13 '08 #5
Well... That went fast.

I searched for eval(), and looked in the manual, then I found "execfile()" which basically does what I want; opens file and makes all functions in that file available. (And It works, I invoked the function in file X in the main file :D

Are there any downsides of using this function (execfile())?
Jul 13 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
2
by: Fritz Bosch | last post by:
Hi experts Is is possible to import/manipulate a module such that I can supply its __dict__? I want to supply my own dict subclass object to be filled by the import, e.g. a class like: >>>...
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
1
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
3
by: kwatch | last post by:
What is the condition of module name which is available in 'from .. import ..' statement ? ---------------------------------------- import os print os.path # <module 'posixpath'...
7
by: bambam | last post by:
import works in the main section of the module, but does not work as I hoped when run inside a function. That is, the modules import correctly, but are not visible to the enclosing (global)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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,...

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.