Solved

python execution

Posted on 2014-03-18
6
612 Views
Last Modified: 2014-04-09
Can I ask a very basic question on python. I have just installed python 3.4 on a windows 7 32-bit system. It appears I have both a GUI or command line tools available with the installation. I have a set of python scripts saved to my desktop. How do I execute them though, do I use command prompt to use cd commands to the desktop, and then run python script.py, or do you have to use the python GUI or command line tools. I just opened to command line and tried a basic CD command and it states "invalid syntax".
0
Comment
Question by:pma111
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:gelonida
Comment Utility
You can just doubleclick on the python script and it will be executed.

The only drawback is:
- a console window will open for .py files during execution.
However at the end of execution it will be closed again. Thus you might not be able to see error messages or the program's output

- alternatively you can open plain windows cmd, then CD to your desktop
  and then type the python script's name.

I never used the python GUI and I don't have 3.4 installed so instead of giving you a potentially wrong answer I let others fill in :-)
0
 
LVL 3

Author Comment

by:pma111
Comment Utility
double clicking the py file doesn't work, it flashes up for a second and then closes, I know the script will be asking for some parameters.

If using the cmd prompt, what is the actual syntax to execute the script, do you need to put python before the script name?
0
 
LVL 16

Expert Comment

by:gelonida
Comment Utility
Probably there is a mistake in your script (syntax error undefined variable or python 2 syntax in a python 3 script.

That's probably why you see the cmd window flash up and disappear.
(If you like to you can post the sript)


Let's assume your script is called mytest.py and is located on the desktop.
then you should be able to type following on the cmd prompt:

cd %HOMEPATH%\Desktop
mytest.py

Open in new window


or alternatively just
%HOMEPATH%\Desktop\mytest.py

Open in new window


Normally the file suffix .py should be associated with windows so windows should be able to find the executable.

If this doesn't work, then please tell me the exact error message

Alternatively you should be able to type following with a python 3 version installed

py mytest.py

or just python mytest.py
with python 2 installed and python.exe setup in the serarch path

otherwise it would be something like (assuming python 2.7 installed)

C:\Python27\python.exe mytest.py
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:pma111
Comment Utility
http://www.ntdsxtract.com/downloads/ntdsxtract/ntdsxtract_v1_0.zip

If you extract that zip, the py I am after is dsusers.py

When running py dsusers.py (having run the appropriate CD commands) I get the error "invalid syntax".. line 30.
0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
Comment Utility
In short,
you try to execute python2 code with your python 3 installation.
the statement
print "hello" 

Open in new window

must be
print("hello")

Open in new window

in python 3

If I remember correctly, pepr did write an article at EE about how to install both  versions of python. (python 2 and python 3)

If I remember correctly, you had to install python 2.7 and then again python 3.4.

To verify, that the install worked well and python version detection is working well you had to type on a windows cmd prompt:
ftype Python.File

Open in new window

The output should be

Python.File="C:\Windows\py.exe" "%1" %*

Open in new window

If it is

C:\Users\klausf>ftype Python.File
Python.File="C:\Python27\python.exe" "%1" %*

Open in new window

then version detection will not work.

After having installed both version of python you can add a line at the beginning of the python file to indicate which version of python should be used.

So for python 2 it would be the line
#!/usr/bin/env python2

Open in new window

and for python 3 it would be the line
#!/usr/bin/env/python3

Open in new window

0
 

Expert Comment

by:HiccupZhu
Comment Utility
You can open it in the IDLE, then prees 'F5' to run.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now