Python Loading a file (module) in Python

AI Thread Summary
To load and execute a Python file in a Windows environment, the correct method is to use "import testprogram" without the ".py" extension, as using "import testprogram.py" results in an ImportError. It is advised to encapsulate the code in a function, such as "run()", which can be called after importing the module. Indentation is crucial in Python, and incorrect alignment can lead to errors in execution. If the module is successfully imported, Python will not provide an error message, but if the function cannot be found, it indicates that the function was not defined correctly or the module is not loaded properly. To access the function directly, one can use "testrun.run()" or "from testrun import *" to simplify the call.
nrqed
Science Advisor
Messages
3,762
Reaction score
297
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
 
Technology news on Phys.org
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
 
chroot said:
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

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
 
Hey Patrick,

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

Code:
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
 
chroot said:
Hey Patrick,

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

Code:
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

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

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

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:
nrqed said:
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

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

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

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:
Good job answering your own q! Sorry I didn't get to the thead earlier.

- Warren
 

Similar threads

Replies
11
Views
1K
Replies
2
Views
827
Replies
8
Views
1K
Replies
8
Views
2K
Replies
11
Views
2K
Replies
9
Views
1K
Replies
2
Views
2K
Replies
2
Views
2K
Back
Top