how to compile and run a python file,and how to change a directory in Python25.what should i import first in order to run a python code.like import math and ......

Member Avatar for leegeorg07

for the imports i run it like this:

1)information about program
2)imports
3)needed variables
3)classes
4)other functions
5)usage of program

also you should add in doc strings into your classes to help other users understand and it is helpful for other people if you heavily comment your code

how to compile and run a python file,and how to change a directory in Python25.what should i import first in order to run a python code.like import math and ......

If you are trying to run a python script that is titled my_script.py from the command line (you could alternately double-click the icon but you won't be able to catch any errors), do the following:

# Change into directory containing your script
C:\Python25\python.exe my_script.py

Now this can be greatly simplified if you add C:\Python25 to your PATH variable. There was a post yesterday about doing just. Once that directory is in your PATH you can simply perform python my_script.py Also for changing directories within Python you want the os module, here's an example:

>>> import os
>>> os.getcwd() # Returns the current working directory
'C:\\Python25\\Lib\\site-packages\\wx-2.8-msw-ansi\\wx\\py'
>>> os.chdir( 'C:\\Documents and Settings\\Administrator\\Desktop' )
>>> os.getcwd()
'C:\\Documents and Settings\\Administrator\\Desktop'
>>> os.listdir( '.' )
['787099A0.219.wd_running.zip', '79950653.826', 'BladeGold.lnk', 'ChassisGold.lnk', 'Clearquest user manual reve.ppt', 'condor_cmds.doc', 'double click on this icon if notes crashes.exe', 'Finances.xls', 'mortgage.txt', 'portals.py', 'SystemX.lnk', 'template.py', 'Things I never Use', 'tkUpdate.py']
>>>

You don't have to compile Python programs. It is interpreted/byte compiled, which basically means that it takes care of that. If you are just starting, I would suggest that you use Idle (comes with Python) until you are more comfortable with the language. A somewhat dated link http://www.ai.uga.edu/mc/idle/index.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.