Python files not executed by command prompt - python

So I dont know what happened but my python files are not being executed by the command prompt.
C:/Users/MOHAMMED/Local/Programs/Python/Python39>button.py
Above is what im trying to do. but it keeps giving me errors.
This file does not have an app associated with it for performing this action.
I have been using cmd prompt everyday to execute python files but today it is not working. any help is appreciated.
(I use IDLE to do my code btw)
edit:
So this is what i do when im trying to execute files using cmd prompt.
I go into the directory of where all the python files are stored by using cd command.
2)then i just say the name of the file and it works.
but today i installed pip by using setup program and after pip was installed, i cant execute python file using cmd prompt.

You are trying to execute the wrong way it should be:
python button.py
If that doesn't work then :
The complete path of python.exe can be added by:
Right-clicking This PC and going to Properties.
Clicking on the Advanced system settings in the menu on the left.
Clicking on the Environment Variables button o​n the bottom right.
In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable.
Clicking on New and entering Python’s install directory.
Then again try :
python button.py

Related

Run a Python script from a .command file with Spotlight on MacOS

I am trying to run a Python script from 'Automate the Boring Stuff' on MacOS by opening a .command file with Spotlight. I have been able to successfully run the script in Terminal by navigating to the directory containing the .py file and then entering: python3 mclip.py [keyword] , but when trying to run it with my mclip.command file, terminal opens a new window and displays this:
I'm not sure why it says no such file or directory, because that file definitely exists in said directory. Additionally, even if mclip.command did run successfully, how would I pass to it the [keyword] argument?
The contents of my mclip.command file are:
#!/usr/bin/env bash
python3 /path/to/my/pythonScript.py
and i'm attempting to run it by opening spotlight and typing in 'mclip.command'.
How can I make this work so that I don't have to navigate to the folder containing the .py file every time I want to run the script?
If you want more context, here are some links to the instructions I've been following: First is the instructions for the script itself is under Project: Multi-Clipboard Automatic Messages, and the instructions for creating and running the .command file are under Running Python Programs on MacOS.
Once you have created your mclip.command file:
Go to the Terminal and type cd /your/folder/location/where/command/file/located/. This changes the current directory where the your command file is located.
Then type chmod u+x mclip.command. This will make your file executable.
Now you should be able to run your script on Spotlight. Either:
Press Cmd + space on your keyboard or clic on the magnifying glass on the upper-right corner.
type mclip.command (or just mclip and Spotlight will do the rest) and press enter.
I still have a problem though:
I am unable to pass arguments to my command file on Spotlight.
I would like to do something like, Go to Spotlight and type:
First mclip.command save <keyword> in order to save what is copied in the clipboard to the key.
Then mclip.command <keyword> in order to reuse what has previously been saved.
It seems to be very easy to do it on Windows (press Win-R and type mcb save <keyword>) but somehow it does not seem possible on Mac OS.
Or is it?
I'd love to find a solution or at least a workaround to quickly execute scripts passing arguments directly on Spotlight...
Let us know if you have the answer =)

I cannot run a Sublime Text 3 Python program from Command Prompt

I am going through the Python Crash Course book 2nd Edition and trying to set up Sublime Text 3 programs to run from a terminal. I have made sure Sublime Text is running Python and have saved a simple Print("Hello) program to a file called python_work. Now I try and run the program from Command Prompt,
I enter cd Desktop\python_work and I get the answer The system cannot find the path specified.
The command prompt should show \Desktop\python_work> and I would contiune from there.
Why do I get this message and how can I fix it? (I am using Windows 10)
I was getting The system cannot find the path specified because the location of the python_work folder was not directly located on my desktop, the location was C:\User\Me\OneDrive\Desktop. I either had to move the file directly to my desktop or use the command cd OneDrive\Desktop\python_work. So if you get The system cannot find the path specified go to properties on your folder and check the exact location and try inputting that into command prompt.

How to change the VisualStudio's run shortcut run command (python)

So i recently had a problem that regarded the command that is executed in vsc's integrated terminal every time the little triangular button on top right corner is pressed
It executes a command that consists of file path to python.exe file, and the currently selected file:
[path to current folder]>C:/Users/[username]/AppData/Local/Programs/Python/Python38-32/python.exe [currently selected file]
and i had problems with that, it didn't recognize some modules. So i found out if i do it by running the command python [currently selected file] it works flawlessly.
I found a setting in the python extension that is labeled python.pythonPath, which i edited from original to "python.pythonPath": "python", which i thought would work but it didn't. I have been digging trough the extention settings for some time now, and i have not been able to find this setting.
Is it even possible? Do i do it right? Is it even done by the python extention?
If you use cmd, it can run the Python files properly,it is recommended that you check whether the environment variables are correct. Reference:This.
To better run Python files in vscode, you could set up the vcode virtual environment.
As for the path 'python.pythonPath', you could refer to:pythonpath.

How to change default directory for IDLE in windows?

The installation directory is "d:\python2.7", and every time I open IDLE and click on menu File and Open item, the default directory is also "d:\python2.7". So I have to change the directory to where I want.
Is there any way I can change it? Using configuration file or changing environment variable?
I tried to add PYTHONPATH in environment variable, but it doesn't work.
I also import os, and use os.chdir(), but it only changes the working directory, not what I want.
Thank you.
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
In response to the answer by Aya:
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
Yes you can, however Python IDLE will no longer run. I've found that if the Start In directory for the IDLE shortcut is C:\Python33 or C:\Python33\Lib\idlelib (in my case) that it will still work but if I added a new directory below either of these (for example: myfiles) containing my programs then the IDLE editor fails. In my situation, I've spend days now trying to get Python to easily get to my .py programs by clicking FILE+OPEN in the IDLE editor. Also, I've tried everything to get IDLE to run without failing from the Windows Explorer using right click and selecting "edit with IDLE" but although Idle does opens okay trying to RUN a program from there fails. I did manage to get IDLE to work using SEND TO and until I get an answer to my IDLE issue Post that is what I suppose I will have to do. Hope this helps.
Open the idle or PyShell
press Alt + M or File -> open Module
type in idlelib.IOBinding
the window opens, go to line 185
change either 'dirname = None' or 'filename = None' to the value you desire.
Eventually you need to start the PyShell or the editor with rights to edit this module like
sudo idle
if you can not edit it you need to see idle.pyw in the same directory an trace how IOBinding is used by the idlelib.PyShell.main() function and change the same variable.

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories