can someone help me get python to run my scripts in CMD in windows 10 without having to cd to exact path each time.? - python-3.x

I'm trying to get python to run my scripts via CMD line.
note: idr if the book said where (a specific place to store my files for) python to access them but scanning back over the beginning I didn't find any relation to it.
According to this book https://automatetheboringstuff.com/2e/appendixb/ I am supposed to be able to type python (script.py) in command line just like this and it should run the script:
Here's the error I am getting upon execution, compared to the example from the book below it to show that this is supposed to work.
CMD LINE OUTPUT :
Microsoft Windows [Version 10.0.18362.1016]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\Armagon>python conway.py
python: can't open file 'conway.py': [Errno 2] No such file or directory
C:\Users\Armagon>
As you can see I get a python Error and researching this has given me nothing I found useful.
Here is the exact sample quoted from the book:
Microsoft Windows [Version 10.0.17134.648]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Al>python hello.py
Hello, world!
C:\Users\Al>
First off, doing some research I have found a way to make it work, by cd(change directory) to the direct path of the folder containing the scripts C:\Users\Armagon\Desktop\mystuff. This helps to verify that the script I am trying to run is in fact located there and I've spelled it correctly.
But, according to everything I've followed up to this point I shouldn't have to do it this way.
The scripts are on my desktop all in the same folder called mystuff.
Following this link as well as a video Add a directory to Python sys.path so that it's included each time I use Python
I added the mystuff folder to PYTHONPATH in environment variables exactly as suggested. Here is a partial output of the sys.path (ran from IDLE) that shows mystuff filepath is added, so I'm pretty sure I did that part right.
['', 'C:\\Program Files\\Python38\\Lib\\idlelib', 'C:\\Users\\Armagon\\Desktop\\mystuff']
Maybe I've confused what is supposed to be happening here
I am operating under the assumption (based off what I've learned so far) that when CMD executes the line: python (script).py
It's supposed to run python.
Which in turn python is supposed to find my scripts on desktop (by the PYTHONPATH environment variable I created) and execute the script typed in CMD.
I've tried a lot of things in the process of getting just this far. I'd really appreciate if someone could point out what it is I'm overlooking to run python scripts from my desktop just like the book. I'm not very skilled at present and will gladly add any information as needed if I have excluded pertinent info I apologize in advance.

You should be able to do this as long as the script is in the module search path, for example, the PYTHONPATH environment variable. You should then be able to run the script like this:
python -m script

Well, the cd way is the way to go to run a python file via the command prompt. There is no other way I can think of.
Follow these steps or this link -> https://www.wikihow.com/Use-Windows-Command-Prompt-to-Run-a-Python-File to do it:
1)Open command prompt. type cmd or command prompt in search bar or run and press enter key.
2)In command prompt, given that the folder you are trying to access is in desktop, type-> cd desktop\myStuff.. Press enter.
3)Just write- python filename.py and enter.
4)It should run automatically.

Related

Why does cmd and powershell recognize "python" but not "py"?

I seem to be having the opposite issue as this user in this question. I can run python successfully, but py does not work for me. Overall, this is not a big deal but when I try to run code in VS Code, I get the error below:
py: The term 'py' is not recognized as a name of a cmdlet, function, script file, or executable program.
How can I either make VScode run python or change my system to accept py?
When I first downloaded Python, it was not placed into the path but I was able to fix that. I also confirmed that VS Code has selected the correct interpreter for Python in the same place.
EDIT: IMAGES
Path directory
Path used in enviroment
When you write "python" on CMD or Powershell, it checks the given keyword("python" here) in all the directories in Environment Variables.
In the case of python, the directory mentioned for python in Environment Variables > Path, you will find "python.exe" and not "py.exe".
So your cmd or PowerShell recognizes python and not py.

What is the cause of "Bad Interpreter: No such file or directory"?

I have a Python virtual environment on my linux machine. It has been working fine for two weeks, but all of a sudden I woke up today, and while in the environment I can't execute any commands. For example, if I try to use pip list, or jupyter notebook, this is what I get (env is the name of my environment):
~/env/bin$ pip list
-bash: /home/ubuntu/env/bin/pip: /home/ubuntu/env/bin/python: bad interpreter: No such file or directory
The same thing happens with basically any other command, except Python. Typing python brings up the Python shell just fine. Interestingly it says Anaconda though, when I only used pip with this environment.
I've tried to find info on this but they all seem to be pertaining to running scripts.
Edit: Also want to mention that when I manually look in the environment bin, the packages I installed are all there in green, except Python is in red.
Thank you in advance.
You have a script /home/ubuntu/env/bin/pip and the script has shebang #!/home/ubuntu/env/bin/python but the file is either absent or is not executable.
Check if the file /home/ubuntu/env/bin/python exist. Check if it can be executed by the current user (just run it from the command line). If not — you need to find out a working executable (for example, it could be /home/ubuntu/env/bin/python3), edit the first line of /home/ubuntu/env/bin/pip to fix the shebang.

Translating Linux command prompt to windows PowerShell prompt

Quick background: I previously used a remote Linux server for a project. Here I was able to pull up the Linux command window in the directory where files from another program were located, and enter a command such as lspp c= example_file.cfile -nographics. LSPP in this case is short for LS PrePost - through this command I was able to call LSPP in the background and have it reference the given *.cfile which contains various commands to be executed, such as depositing a given *.csv file with results, its name, etc.
I recently managed to install LSPP for windows locally, and am now trying to essentially translate the functionality of the command I executed in Linux, to Windows. I should add that I have no experience with CMD or Windows PowerShell...
The first thing I tried was to use cd C:\.... to set the directory to where the *.cfile is located and then enter the same lspp c= example_file.cfile -nographics from Linux, however, the response was that "lspp" was either spelled wrong or could not be found.
Next I tried setting the directory to the LSPP program folder, and using start LS-PrePost-4.7-x64 to at least start the program, but this didn't work either.
I would really appreciate it if someone could point me in the right direction here - thank you!

Trying to test Python on Win 10, getting Errno 2. Path to python is given to the value of env variable

I've installed Python 3.8 on Win 10 machine. Path to Python is C:\Users\userid\AppData\Local\Programs\Python\Python38\
Path environment variable values are: C:\Users\userid\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\userid\AppData\Local\Programs\Python\Python38\python.exe
In order to test python, I created a simple script called "script.py" in directory C:\python_scripts.
Then I tried to run "script.py" from the directory where it is located, but got error:
C:\python_scripts> python script.py
C:\Users\userid\AppData\Local\Programs\Python\Python38\python.exe: can't open file 'script.py': [Errno 2] No such file or directory
What might be the cause of getting this error? i tried to run the script with both cmd and powershell, with the usual user rights and with elevated admin rights, still getting Errno 2.
No other versions of Python have been previously installed on this machine (which is a corporate one, if that might be significant)
Thank you
Thank you #OferSadan for your suggestion to use dir command, it helped me to solve the problem. The "script.py" had an additional .txt extension added to it for some reason, but the Windows explorer only showed .py extension by default. Now I deleted the trailing .txt suffix and the script runs

Open python IDLE in specific Windows directory?

Issue: IDLE always starts using a specific windows directory: 'C:\Python36'. That is my installation directory. I do not want to keep my project files in that directory. I want files for a specific project in a separate project directory.
After hours of searching, I did not find an answer to my specific question. I did find a helpful clue in How to start IDLE (Python editor) without using the shortcut on Windows Vista?
The solution that meets my needs is to create a file: idlehere.py
import idlelib.pyshell
idlelib.pyshell.main()
I then place that file in my project directory and execute the file with
python idlehere.py
This starts IDLE in my project directory.
Even though this works for me, I am posting this question in case it helps others; or in case there is a better way to accomplish my objective.
UPDATE: From the comment and with further experience I have learned additional ways to start IDLE in a specific Windows directory.
For each case open a command prompt in the desired directory.
At the command prompt, enter python. That starts python in interactive mode.
To start idle enter: import idlelib.idle
At the command prompt, enter python -m idlelib
At the command prompt, enter python -m idlelib.idle

Resources