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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ManWithNoName
    New Member
    • Aug 2007
    • 88

    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.

    Code:
    f=open('.py')
    import whateverThatCanDoThis
    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 ;)
  • tharden3
    Contributor
    • Jul 2008
    • 916

    #2
    ooh, that sounds tricky... I donno. I've never tried to import something with no name

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Any chance you can rename the file to something sane? That'd be the simplest solution here...

      Comment

      • tharden3
        Contributor
        • Jul 2008
        • 916

        #4
        Isn't it a bit ironic that your profile name on this website is ManWithNoName?

        Comment

        • ManWithNoName
          New Member
          • Aug 2007
          • 88

          #5
          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?

          Comment

          • ManWithNoName
            New Member
            • Aug 2007
            • 88

            #6
            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())?
            Last edited by ManWithNoName; Jul 13 '08, 08:16 AM. Reason: while x>y: edit file

            Comment

            Working...