Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Loading a file (module) in Python

  1. May 13, 2006 #1

    nrqed

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I have installed Python in a Windows environment and I want to load files to execute them in Python.

    I have tried
    "import testprogram.py"

    and it does load the program and executes it (all the program does is to print something out on the screen) but then there are error messages:


    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in -toplevel-
    import testprogram.py
    ImportError: No module named py


    Why is there an error message? is there another way to load and execute a program?

    Also, how do I get to execute the program a second time? I cannot get it to execute a second time!

    Thanks
     
  2. jcsd
  3. May 15, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You don't

    import testprogram.py

    you just

    import testprogram

    The python interpreter then goes and looks for a file which contains a module called testprogram, which would ordinarily be in a file called testprogram.py.

    Using import to run another program is a very poor design. Import should not run anything; it should just bring entities (like methods and so on) from one module into another so they can be used there.

    Your code should be in a method, perhaps called run. After you import that method, you can call it once (or twice).

    - Warren
     
  4. May 15, 2006 #3

    nrqed

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Ok. I appreciate this very much. I am just starting to use it so it's no surprise I went about it the wrong way. I was looking at the book "Learning Python" (O'Reilly edition) and they start by explaining two ways to run programs: directly from the Python Command Line in interactive mode (which means that all the work is lost upon exiting) or typing the commands in a text file and running it from the Dos (say) command line using "python nameoffile". I wanted to do something intermediary: having my program in a text file but loading it in the Python environment and running it there. I could not find how to do this from the book except using import.

    Your comment is very much appreciated. I hope you will be available in the near future for other Python questions.

    Best regards

    Patrick
     
  5. May 15, 2006 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hey Patrick,

    I suggest putting your code in a run method like this:

    Code (Text):
    def run():
        ...
        ...
     
    To run your code, import the module, then call the run() method.

    I'll be around for any python questions you might have.

    - Warren
     
  6. May 17, 2006 #5

    nrqed

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Hi Warren,
    Thank you very much. I appreciate it. It is very frustrating to try to not even be able to do some simple things:mad:

    I tried this and was not able to get it to work. I simply wrote a file named testrun.py and just typed

    def run():
    print "this is a test of using run"

    (this does not come out the right way..I aligned the "p" with the "r" of run)

    (I also tried later by including a return statement after the print but it did not work either).

    Now, on the Python command line I typed "import testrun" and that went through with no error message (thanks for pointing out that the py extension should not be included).

    But then, I tried typing "run()" ( or simply "run") and it keeps telling me that it does not know what "run" is:grumpy:

    So here are my questions:

    a) would Python tell me if it did not find the file tesrun? Or would it not say anything, giving me the wrong impression that it was loaded (I am starting to wonder if I saved it in the right directory). I was assuming that it had found since there was no warning message.

    b) Indentation is crucial in Python, isn't? If my statement "print" is not indented correctly, would that cause a problem? (I aligned the "p" with the "r" of "run"

    c) Is a "return" statement mandatory at the end of my function "run"?


    Sorry for the dumb questions. For some reason, it seems impossible to learn a language just from a book, it always seems like it is impossible to get something working without someone's guidance :grumpy:

    I appreciate very much your help. I am trying to use Python to code some QCD calculation, so it is frustrating not to even get something as simple as this working!!

    EDIT: another question: if I load a file with a function defined in it (like my run), how can I get the code of the function within the Python environment? In this example, how could I see that the code is Print"..." ?


    Regards

    Patrick
     
    Last edited: May 17, 2006
  7. May 17, 2006 #6

    nrqed

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Never mind!! I figured it out (I had to type testrun.run() obviously). Sorry for the dumb questions.

    And thank again for helping out. I am sure that more questions will follow!

    Patrick

    EDIT And I realized that I could also use "from testrun import *" in order to simply use run() afterward.
     
    Last edited: May 17, 2006
  8. May 17, 2006 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Good job answering your own q! Sorry I didn't get to the thead earlier.

    - Warren
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Loading a file (module) in Python
  1. Loading a File in C++ (Replies: 15)

  2. Java or Python (Replies: 13)

  3. Learning Python (Replies: 4)

Loading...