I am trying to run a program from python command line. I installed python on D: drive. PLZZ help me in fixing this problem.

Recommended Answers

All 10 Replies

Hi,
May be, os.system() is useful to you:
Click Here
Forgive me if I am wrong.

Sorry. i forgot to upload the screenshotClick Here

I believe that what you are looking for is the import statement. If you have a Python file, say test.py, and wish to run the program in that source file while in the interpreter prompt, you can enter

import test

and, provided that the file test.py is in the same directory that you launched the Python interpreter from, you should be able to run the program, as well as access it's variables, call it's functions, etc. once it is imported.

Mind you, this is not the usual manner in which Python programs are run. It is more typical to have the .py files associated with pythonw.exe, which allows you to run the program just by double-clicking on the file. See the link Gribouillis posted for more details.

If, rather, you are looking to edit and test the program in question, you would be better off running IDLE (and simple integrated development environment that should have come with your Python distribution), which has a window for the interpreter prompt but also allows you to open separate editing windows for working on your source files. If you launch IDLE, the go to File-> Open File... and select your test.py file, it will open a window showing the source code; you can then run the program by going to Run-> Run Module in that window. IDLE takes a bit of getting used to, so you might consider a more powerful IDE such as Dr Python (very simple to learn and use, but only supports Python 2.x, and you need to install wxWindows before running it), PyPE, or Eric (much more powerful and complete, and a bit tricky to set up as you need to install both Qt4 and PyQt4 before running the installation).

I dont know what is the problem behind it. Actually i have an assignment in which i have to test from python command line.
For example i have to test python test.py -l
what does it mean.

That command is for shell/cmd.exe (WindowKey-R cmd <enter key>) not to python read-eval-print loop of interaction aka Python prompt (>>>)

Let's say you have this program saved as test.py in folder "C:/Python27/Mytests/"

# save as test.py
for x in range(10):
    print(x)

Now you can do this in the Python shell and see the output...

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> execfile("C:/Python27/Mytests/test.py")
0
1
2
3
4
5
6
7
8
9
>>> 

In your case specify the D: drive.

commented: Thank u. +1

In python 3 it would be

>>> exec(open("test.py").read())

instead of execfile.

Thank you sooo much for all.

thanks to all for your helps

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.