Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

python execution

Posted on 2014-03-18
6
Medium Priority
?
638 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 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

886 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