hey guys im trying to teach myself python at the moment and when i finish writing the code and go to run it in an interpreter i always get this error
Traceback (most recent call last):
File "<input>", line 1, in ?
NameError: name 'expression' is not defined
>>> python expression.py
File "<input>", line 1
SyntaxError: invalid syntax
i dont know what the exact cause of this. but i know that i am inputting the code correctly.[/code]
Run it like this:
C:\> python expression.py
should i be putting my *.py files in a certain area for saving? or should it not matter where i save them?
|lastelement0 wrote: |
|should i be putting my *.py files in a certain area for saving? or should it not matter where i save them? |
Doesn't matter. But you must either run python from the directory in which the .py file is, or specify the complete path to the .py file.
C:\> python \mydir\pythonfiles\test\expression.py
how about in terms of using an interpreter?
because currently im using ActiveGrid which allows me to write my python programs
then use an interpreter within it to run them. however thats where i get those above errors. i assumed it had something to do with the location of the file but idk where to put the file or make it so i can run it.
Ai, I can't help you with that. I don't know ActiveGrid. Maybe you can configure the way python is called, and specify that the file you are working on has to be supplied as an argument passed to the parameter?
how would i go about changing how python is called?
I'm not sure if this is what you need help with, but it is possible to import a module into your interpreter quite easily. First you should add the path from which python checks the modules, this is done like this:
>>> import sys
['C:\\Programmering\\Python', 'C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages']
['C:\\Programmering\\Python', 'C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Programmering\\Python\\Beginning_Python']
Then you can do:
You would access the functio "stuff" inside the module "add" like this:
This allows you to call all the functions, classes etc in your module from the interpreter. Does that help?
When you run a python program from within the command prompt of your OS (such as the DOS-like cmd.exe in Windows), Python interprets the file and runs it stand-alone. When you import the module from the Python interactive environment, it only imports the things defined, and you must call the functions separately.
I think what you're trying to do is run a Python script stand-alone, but you're trying it from the Python interactive environment. That won't work; technically, you're already within "python" (the interactive environment), and trying to call "python" (the stand-alone programs interpreter). Honestly, how does that make sense?
The interpreter takes python code one line at a time and uses it. So, "x = 1" then "y = 2", each on one line, first sets x to 1, then y to 2, all interactively; that is, while you're actually there typing the lines one at a time. Then you could wait a few hours and type in z = x + y, and the interpreter would still be waiting.