Loading a file (module) in Python

  • Context: Python 
  • Thread starter Thread starter nrqed
  • Start date Start date
  • Tags Tags
    File module Python
Click For Summary

Discussion Overview

The discussion revolves around loading and executing Python files (modules) in a Windows environment. Participants explore the correct usage of the import statement, the structure of Python code, and address common issues faced by beginners.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant initially attempts to import a Python file using the syntax "import testprogram.py" and encounters an ImportError, prompting questions about the correct method to load and execute a program.
  • Another participant clarifies that the correct syntax is "import testprogram" without the .py extension, emphasizing that import should not execute code but rather bring in functions or classes for use.
  • There are suggestions to encapsulate code within a function (e.g., "run()") to facilitate execution after importing the module.
  • Questions arise regarding the visibility of functions after importing, the importance of indentation in Python, and whether a return statement is necessary at the end of a function.
  • A participant expresses frustration with learning Python from a book and seeks guidance on practical execution of code.
  • Later, the participant realizes the need to call the function with the module prefix (e.g., "testrun.run()") and shares this discovery.

Areas of Agreement / Disagreement

Participants generally agree on the correct usage of the import statement and the importance of structuring code properly, but there is no consensus on the best learning methods for beginners, as experiences vary.

Contextual Notes

Some limitations include potential misunderstandings about file locations, the necessity of correct indentation, and the handling of function visibility after import, which remain unresolved in the discussion.

Who May Find This Useful

Beginners learning Python, particularly those interested in module usage and execution in a Windows environment, may find this discussion helpful.

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 ·
Replies
11
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K