python execution

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".
LVL 3
pma111Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gelonidaConnect With a Mentor Commented:
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
 
gelonidaCommented:
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
 
pma111Author Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
gelonidaCommented:
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
 
pma111Author Commented:
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
 
HiccupZhuCommented:
You can open it in the IDLE, then prees 'F5' to run.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.