Loading a file (module) in Python

In summary,- There is an error message when trying to load a file to execute in Python- The way to load a file and execute it is to import it and then call the run() method
  • #1
nrqed
Science Advisor
Homework Helper
Gold Member
3,766
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
  • #2
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
 
  • #3
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
 
  • #4
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
 
  • #5
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: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:
  • #6
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: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

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

- Warren
 

1. What is the purpose of loading a file (module) in Python?

Loading a file (module) in Python allows you to access and use functions and variables from that particular file in your current program. This can help organize your code and make it more modular.

2. How do I load a file (module) in Python?

To load a file (module) in Python, you can use the "import" statement followed by the name of the file. For example, if the file is called "my_module.py", you would use "import my_module" in your code. This will make all the functions and variables in that file available to use in your program.

3. Are there different ways to load a file (module) in Python?

Yes, in addition to using the "import" statement, you can also use "from" to specify specific functions or variables to import from the file. For example, "from my_module import my_function" will only import the specified function from the file. You can also use aliases to rename the file or specific functions/variables for easier use.

4. Can I load multiple files (modules) in one Python program?

Yes, you can load multiple files (modules) in one Python program by using multiple import statements. You can also use the "as" keyword to specify aliases for each file or function/variable from that file.

5. Is there a specific order in which I should load files (modules) in Python?

It is recommended to load built-in modules first, followed by third-party modules, and finally your own custom modules. This helps avoid conflicts between functions and variables with the same name in different modules.

Similar threads

  • Programming and Computer Science
Replies
8
Views
870
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
9
Views
852
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
506
Replies
6
Views
620
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top