How to run python script on terminal (ubuntu)? - python

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

Related

zsh: killed code . command on MacOs M1 chip monterey machine

So, I was trying to open vscode from terminal writing the following:
code .
so, I got the following
/usr/local/bin/code: line 6: python: command not found
/usr/local/bin/code: line 10: ./MacOS/Electron: No such file or directory
so, I tried to edit this file using vim sudo vim /usr/local/bin/code and changed the python to python3 and saved using :wq!
it didn't edit it as it's a read-only file, however, I open it as a root user who has a write-permission, giving me an error that
E166: Can't open linked file for writing,
and now when I try to do code . in the terminal it gives me
zsh: killed code .
and it's kinda stuck at this phase, now how can I open vscode from the terminal given that scenario?
code is a read-only file that already had been added to $PATH. It originally located in /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/ given that you are on a Mac machine.
to fix
/usr/local/bin/code: line 6: python: command not found
you would need to change python to python3 using any editor giving code file the right permissions so you can edit it.
so,
sudo chmod 774 code now you have permission to edit it.
change python to python3 and save the file.
restart the terminal.
and now, it should work fine, as it did for me.
Use shortcut keys "CTRL+SHIFT+P" in your vscode.
Choose command "Shell Command: Install 'code' command in PATH"
Restart the terminal for the new value to take effect. You'll be able to type 'code .' in any folder to start editing files in that folder.
You can refer to the document for more details.

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

Running Python on Ubuntu Shell

I am trying to run python on Ubuntu. ADDED: It's a double boot system with Windows.
If I type python on the shell, it opens python. But I want to run a python file.
I have my python (.py) file saved on windows Desktop.
On Windows when I run a .py file in my command prompt, I just have to change directory to Desktop then type python myfile.py and the code runs successfully.
When I try to do that same thing in Ubuntu, it does not work. I made a Desktop directory in Ubuntu using the mkdir function.
Now, when I type python myfile.py, I get the error:
python: can't open file 'myfile.py': [Errno 2] No such file or
directory
I tried typing python on the Ubuntu shell then dragging the myfile.py file (~$ python C:\Users\username\Desktop\myfile.py), it used to run and then close the answer right away, but now I would get the error
python: can't open file 'C:UsersusernameDesktopmyfile.py': [Errno 2]
No such file or directory
Can anyone tell me what are the exact steps I need to do to fix this?
EDIT:
Here is what I am writing from the answers below:
~$ python /home/username/Desktop/myfile.py
Yet, I am getting this error:
python: can't open file '/home/username/Desktop/myfile.py': [Errno 2] No such file or directory
EDIT 2**
So here is something new:
if I write
python /home/username/Desktop myfile.py
I get this error
/usr/bin/python: can't find 'main' module in '/home/username/Desktop
If I write
python /home/username/myfile.py
I do not get an error, but I do not get any output either. Ubuntu just goes to the next line $
Strange on Ubuntu your path starts with c:\...
On Ubuntu user folders are usually in /home, user folder can be referenced by ~, so IMHO python ~/Desktop/myfile.py should work in your environment.
EDIT: noted you made Desktop folder, not original Desktop, that way when you are in that folder type pwd it will show full path, then put it in python PATH/myfile.py (and just in case you may type ls to show list of files in current folder on Linux to check you are indeed in the right folder where your program is).
ADDED: after discussion it turned to be double boot system, mount showed mounted windows disk and file.py was found and run!
Backslash '\' is an escape character, in Unix it will not be used if you put it in a path. It results in your error of path not found.
Use slash '/', your code should run.
It is weird that you have a path for windows, in Unix you should not have this type of path...
Open a terminal, go to the folder with your python script. Use pwd in the terminal to know the exact location and then copy the path and use the following (I take an example here):
python PATHTOYOURPYTHONSCRIPT/mypythonscript.py
You're trying to use windows path representation in Linux. Windows and Linux has different path representations.
In windows, you can use C:\ but in Linux it's just a / which is used to denote the root directory.
In the terminal, type 'pwd' where your python file is present in Ubuntu and you'll see the output as '/home/username/Desktop' which is not like windows.
So you need to run like, 'python /home/username/Desktop/my file.py'.
If you need to access the file present in the windows partition, you need to mount the windows partition. This can be done using the Files app present in Ubuntu. After that, you can navigate to /mnt/media/ and find your file.

Running Shell Script from Applescript

I want to run a Shell script using Automator in OSX. The shell script calls a Python file which is located in the folder "/Users/Tex/Python". My code is:
on run {input, parameters}
tell application "Terminal"
do shell script "/Users/Tex/anaconda3/bin/python /users/Tex/Python/MoveFiles.py $#"
end tell
return input
When I execute the automator script I get an error as follows:
Terminal got an error: /users/Tex/anaconda3/bin/python:
can't open file '/users/Tex//Python/Move_Files.py': [Errno 2] No such file or directory
I suspect that the problem is that the "/Users/Tex" folder is located on a different drive to the OSX operating system but that the Shell interpreter is not aware of this. I have tried using the full path to the python file, i.e. including the volume name (iMac Disk A) however the fact that this has spaces in it seems to create yet another issue for the Shell.
Appreciate feedback on how to make thsi work!

script file not found when using cmd.exe

I'm just getting started with Python and am trying to run a program from the command line, as it is done on this website under the heading "Python Program". So I made script hello.py, it's located in my computer at C:\Python27.
In the example, they run the script by typing python hello.py Guido. When I try to do this, it doesn't work. Firstly, I'm not entirely sure what is meant by the 'command line', but I'm using cmd.exe in Windows XP. I get this:
python: can't open file 'hello.py': [Errno 2] No such file or directory.
I have already specified PATH as C:\Python27.
Also, when I try to run the program from the Python shell by typing hello.py Guido I get
SyntaxError: invalid syntax.
When you start cmd.exe, the default directory is your Documents and Settings: since your file hello.py is not there, the python interpreter can't find it, thus giving you the [Errno 2] No such file or directory error. To solve that, just change your current working directory:
C:\Documents...>cd C:\Python27
C:\Python27> python hello.py Guido
Anyway, it is a good approach not to having your files inside the python directory (create a directory in your documents for python sources and use the same approach).
When you are running the python shell, you cannot explicitly call python files, so in your case it tries to run hello.py as a command (which doesn't exists) and it gives you a syntax error.
You need to locate your cmd current directory at C:\Python27:
cd C:\Python27
because the path python loads is relative. You can also use a full path:
python C:\Python2.7\hello.py
Try without "python", when you put python directory in path, it automatically connects ".py" extension with python, so there is no need in writing "python hello.py Guido"
Just go to directory where .py is located, and call "hello.py"
What's your current working directory and where is hello.py located? To execute that command, hello.py should be in the same directory from where you started the commend line (cmd.exe). Otherwise you need to the write the absolute path of hello.py (like python C:.....\hello.py Guido) instead of just the filename 'hello.py'.
I had also this problem but because of ether reason: I accidently added spaces to the names of some of the file's names, so the CMD didn't recognized the names.
for example: 'run .bat'

Categories