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

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.

Related

I want to make .exe file, but facing an error

Here I'm using python 3.9.0
pyinstaller : The term 'pyinstaller' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
So pyinstaller isn't on your path
According to WikiPedia A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory.
Checkout this Question from Stackoverflow it will help you
The problem here is that you are not in the shell but in Microsoft PowerShell. That's something completely different. Even if it looks a little like a command shell.
Exit the PowerShell and open the command shell by pressing "Windows Logo"+R, enter cmd and hit OK. Then, you can execute pyinstaller.
Alternatively (and sometimes much easier), you can create a Batch file to execute the pyinstaller command line with all necessary options. Just create an empty text file, write the command in it, write pause on the last line and store it with the extension .BAT. You can then double-click it to execute the command. (pause is necessary to keep the terminal open until you have read all messages, warnings and errors)
I resolved it by this process.
Run this command in the respective path where you got an error.
pip uninstall pyinstaller
go to main python folder C:\Users\USER NAME\AppData\Roaming\Python
and open powershell window in the python path and Now run this command.
pip install pyinstaller

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 =)

Python files not executed by command prompt

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

Changing the username command in terminal in Mac

I am using terminal to try to run my code and I got it to run yesterday, but the username was tests-MacBook-Pro-2:sunkunoil maryjacketti$ and today it is tests-MacBook-Pro-2:~ maryjacketti$ and it won't run the code. The code is inside the folder sunkunoil on my computer. How do I get the terminal to say prompt the command like yesterday?
You have to change the directory that you are working in. This isn't an issue with the user that you are using, but rather where you are in the file system.
cd /desktop/sosim/sunkunoil
python [what ever your program name is]
The structure of terminal is:
[machine]: [current folder] [user]$
You are currently in home which is represented by ~.
On a Mac, the paths are at different locations. For posterity, you can either change to a relative path
cd SOSim/sunkunoil
or an absolute path
cd /Users/maryjacketti/SOSim/sunkunoil
The first part is where you start out when you start the Terminal app. It is also called you HOME directory.

How to run python script on terminal (ubuntu)?

I'm new with python, I've been learning for a few weeks. However now I've just changed my OS and I'm now using ubuntu and I can't run any script on my terminal.
I made sure to have the #!/usr/bin/env python
but when I go to the terminal and type, for example python test.py
the terminal shows an error message like this
python: can't open file 'test.py': [Errno 2] No such file or directory
what do I do?
I must save the file in any specific folder to make it run on terminal?
This error:
python: can't open file 'test.py': [Errno 2] No such file or directory
Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)
I must save the file in any specific folder to make it run on terminal?
No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.
Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".
Thus, if you type something like:
python test.py
test.py needs to be in the current working directory. In Linux, you can change the current working directory with cd. You might want a tutorial if you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bash for a shell, so it should apply to you.)
Set the PATH as below:
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.
In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python" and press Enter.
Note − /usr/local/bin/python is the path of the Python directory
now run as below:
-bash-4.2$ python test.py
Hello, Python!
Save your python file in a spot where you will be able to find it again. Then navigate to that spot using the command line (cd /home/[profile]/spot/you/saved/file) or go to that location with the file browser. If you use the latter, right click and select "Open In Terminal." When the terminal opens, type "sudo chmod +x Yourfilename." After entering your password, type "python ./Yourfilename" which will open your python file in the command line. Hope this helps!
Running Linux Mint
Sorry, Im a newbie myself and I had this issue:
./hello.py: line 1: syntax error near unexpected token "Hello World"'
./hello.py: line 1:print("Hello World")'
I added the file header for the python 'deal' as #!/usr/bin/python
Then simple executed the program with './hello.py'
First create the file you want, with any editor like vi or gedit. And save with a .py extension. In that the first line should be
#!/usr/bin/env python

Categories