FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


python issue





lastelement0
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

Code:
>>> expression.py
Traceback (most recent call last):
  File "<input>", line 1, in ?
NameError: name 'expression' is not defined

or this

>>> python expression.py
  File "<input>", line 1
    python expression.py
                    ^
SyntaxError: invalid syntax


i dont know what the exact cause of this. but i know that i am inputting the code correctly.[/code]
MrBlueSky
Run it like this:

Code:

C:\> python expression.py
lastelement0
should i be putting my *.py files in a certain area for saving? or should it not matter where i save them?
MrBlueSky
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.

Code:

C:\> python \mydir\pythonfiles\test\expression.py


for example.
lastelement0
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.
MrBlueSky
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?
lastelement0
how would i go about changing how python is called?
qebab
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:
Code:

>>> import sys
>>> sys.path
['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']
>>> sys.path.append("C:\\Programmering\\Python\\Beginning_Python")
>>> sys.path
['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:
Code:

>>> import add


You would access the functio "stuff" inside the module "add" like this:
Code:

>>> add.stuff()

This allows you to call all the functions, classes etc in your module from the interpreter. Does that help?
Twisol
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? Wink

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.
Related topics
Python via CGI - A personal triumph
Python via CGI - A personal triumph
problem with python's os.mkdir in cgi test
Monty Python
Python Tutorial
Python
python tutorials
python tutorials
usb external HD
Python books
Python Bursts After Trying to Eat Gator
python support
cpanel q: Doesn't seem to be a firewall issue
Building my own comp
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.