Python Forum
how to run a file in powershell with python
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to run a file in powershell with python
#1
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?
Reply
#2
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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?
Reply
#4
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(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
Reply
#6
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.
Reply
#7
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using PowerShell from Python script for mounting shares tester_V 8 507 Mar-12-2024, 06:26 PM
Last Post: tester_V
  PowerShell & Python deep_logic 2 693 Jun-06-2023, 06:34 AM
Last Post: buran
  How to write a part of powershell command as a variable? ilknurg 2 1,118 Jul-26-2022, 11:31 AM
Last Post: ilknurg
Photo Windows 10 PowerShell doesn't work Amy 3 3,911 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  Powershell Session translation to Python; Session code seems to not work Maverick494 1 3,594 Jun-26-2018, 05:16 PM
Last Post: Maverick494
  virtualenv on powershell mcmxl22 3 6,358 Feb-08-2017, 06:29 AM
Last Post: snippsat
  Cant Get Powershell to run a python program from notepad++. Newbie help! Pythonerous 4 8,901 Oct-10-2016, 07:16 PM
Last Post: Pythonerous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020