Python Forum

Full Version: how to run a file in powershell with python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to run my hello world in python and im using powershell as my terminal and notepad++ as my register and I keep seeing this error message

Windows PowerShell
Copyright © 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Home> python ex1.py
C:\Anaconda3\python.exe: can't open file 'ex1.py': [Errno 2] No such file or directory
PS C:\Users\Home> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ex1.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ex1' is not defined
>>> python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> cd temp
File "<stdin>", line 1
cd temp
^
SyntaxError: invalid syntax
>>>

can anyone help me with this?
You are mixing things up. To run your program you basically have two choices:
1) You need to navigate to the directory containing you file. then type the entire file name including the ".py"

For example

PS C:\Users\sparkz> cd c:\python
PS C:\python>ex1.py
2) In file explorer, navigate to your file and double-click it.
Notice your prompt is "PS C:\"
To use python in interactive mode, type "python" (if you have one version of python installed)

For example:

PS C:\> python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Notice in this mode, your prompt is now ">>>"

To exit this mode and return to the command prompt, type "exit()" then Enter
Thank you So much for your reply and I did your instructions to the letter. I found it beneficial however, I'm still unable to run my hello world.py or what I had to name the image as exl.py. I'm reading the book learn python the hard way. and I think I should be running python 2 not python 3.2. would this have a impact on the end result?
There are some differences, the two most prominent are v2's 'raw_input()' is now 'input()' and v2's 'print' statement is now a function 'print()'

For example, in v3.x a 'hello_world.py' might contain a single line:
print("Hello World")
There are more differences, but those are the main two that people stumble on.

Quote:... and I think I should be running python 2 not python 3.2

Definitely not, if you are just starting out, learn v3, I will even go so far as to say learn the very latest version (at this point 3.6).  The second thing I would recommend is find a new book, you will find no love, at least on this site, for the book or it's author.  One case in point, why tell the reader to use Powershell rather than the standard command terminal? Powershell is it's own scripting language similar to 'bash' in Linux or Unix. Using it's terminal adds no benefit to Python. In fact, if you were to double-click our 'hello_world.py' script, it will run in the command terminal not PS's terminal (though it will run so fast you will most likely not see it run).

EDIT: Although Windows will allow spaces in file and directory names, it does not in the command terminal nor is it allowed in other OS's. Since Python is cross platform, it is good practice not to use something the is OS specific, unless necessary.
(Jun-27-2017, 11:38 AM)RomanEmpire Wrote: [ -> ]I'm reading the book learn python the hard way.
There has been massive critique against learn python the hard way.
Zed firstly said that Python 3 sucks and he can't teach it, and now he teach it Sick
Learn Python 3 the Hard Way which teaches Python 3

You should start with Python 3.6, have setup part here.
cmd and Powershell is not good at all in option,use cmder.
I have a tutorial about it here
Thank you both for your wisdom, I will not be using Powershell if that is the case. I'm a super novice with this (just learned command lines) and any and all advise is truely appreciated. with that said what would be the use of notepad++? if learn python the hard way is not well respected what would anyone recommend? I'm learning python to program raspberry pi and to do back end development.
As for books and such, a good place to start is here:
Free Python Resources

Notepad++ is Windows only and is an advanced text editor (think much better than Notepad) and is similar to KDE's "Kate" in Linux. It has a Python plugin that provides code highlighting and a certain amount of code completion. Two nice features (in your case) is it allows you to change Windows "CRLF" to Linux's "CR", and to save files as "utf-8". Both are necessary if you are developing on Windows and then copying the files to the Pi. Think of notepad++ as a midway point between a basic text editor like Notepad and a full IDE. If you are running Raspian on your Pi, I believe it comes with a Python IDE called 'Geanie'. As to the full IDE's, they again are a matter of personal choice, I myself use Pycharm Community Edition, not because it is 'better', but it's the one I feel most comfortable with. My advise is to try several and find the one you like best (the fact that it's free doesn't hurt either). Whatever your choice(s) make sure they are still active and well supported.