Solved

python execution

Posted on 2014-03-18
6
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:gelonida
ID: 39936584
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
ID: 39936615
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 17

Expert Comment

by:gelonida
ID: 39936651
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:pma111
ID: 39936729
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 17

Accepted Solution

by:
gelonida earned 500 total points
ID: 39936802
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
ID: 39988064
You can open it in the IDLE, then prees 'F5' to run.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

617 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