I have a python script abc.py. Inside a shell script file, I call it ./abc.py
Inside abc.py, at the top, I have #!/usr/bin/python
test -x abc.py && echo true || echo false return true
On executing the shell script, it says ./abc.py: Command not found.
It works if I call it as python ./abc.py instead of just ./abc.py
The issue is only with this abc.py file that I created today. The older python scripts in the same directory with different purpose and names work without any issue.
I have referred the correct path of the file BTW. What could be the issue?
In tcsh, this happens when the interpreter is invalid:
$ cat foo
#!/invalid
$ tcsh -c './foo'
./foo: Command not found.
$ bash -c './foo'
bash: ./foo: /invalid: bad interpreter: No such file or directory
This could be for several reasons. Perhaps your path is wrong for your system:
% type python
python is /usr/local/bin/python
in this case the script needs to start with #!/usr/local/bin/python instead.
Alternatively, the script could have Windows line endings (carriage returns):
$ cat -v foo
#!/usr/bin/python^M
...^M
In this case, save it with Unix line endings instead.
Trying to replicate your setup as follows:
File: asdf.py
#!/usr/bin/python
print("Hello World")
File: asdf.sh
#!/bin/bash
if [ -x asdf.py ]; then ./asdf.py ; else echo "Arrrrgh. File does not exist or does not have executable permisions"; fi
Now I save the files in the same directory, e.g. the Desktop, make them executable and run the shell script from the terminal.
usr#cmptr $ chmod +x asdf.{py,sh}
usr#cmptr $ ls -la asdf.{py,sh}
-rwxr-xr-x 1 usr usr 613 Mar 15 22:22 asdf.py
-rwxr-xr-x 1 usr usr 56 Mar 15 22:14 asdf.sh
usr#cmptr $ ./asdf.sh
Hello World
Now, maybe I have misunderstood your setup but this should work just fine.
Related
I have a script I have downloaded and installed from GitHub that I am trying to add to my path. Although the script is in the path already somehow I can't run it unless I provide the full path.
echo $PATH
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/me/bin/SCRIPT_FOLDER/
/Library/TeX/texbin
/Users/me/bin/SCRIPT_FOLDER/script.py
Since I am working on Catalina 10.15.7, I saw in other answers that I need to add the script to the .zshrc file, so I did so.
code .zshrc
export PATH=/Users/me/bin/SCRIPT_FOLDER/:$PATH
export PATH=$PATH:/Users/me/bin/SCRIPT_FOLDER/script.py
Despite this the script cannot be run except if I type the entire path
python /Users/me/bin/SCRIPT_FOLDER/script.py --h
What can I try to solve this?
script.py must be executable and the first line must be as shown below.
? echo $PATH
.:./bin:...
? ls
bin
? ls -l bin/script.py
-rwxr-xr-x. 1 x x 40 Mar 9 10:54 bin/script.py
? cat bin/script.py
#!/usr/bin/python
print('Hello world')
? script.py
Hello world
?
If the script is not executable use the chmod command like so:
chmod 755 bin/script.py
Like most command-line programs, Python doesn't consult your PATH for the arguments (e.g., script.py, given the command python script.py). Your PATH only matters when deciding how to resolve the command name (in this case, python). If script.py is in one of your PATH directories (like /Users/me/bin/SCRIPT_FOLDER/), it has the appropriate executable bit set, and it begins with an appropriate shebang line such as #!/usr/bin/env python, then you can run it with the command script.py.
I have a folder named test-folder with two files.
# file name: demo.py
print('hello world')
# file name: script.sh
python3.7 demo.py
The test-folder is present inside /home/username/Documents/
I have exported the above path to the .bashrc.
Now when I try to execute the script.sh using the following command,
bash script.sh
I get the following error
python3.7: can't open file 'demo.py': [Errno 2] No such file or directory
How can I solve this? I'm on Ubuntu 18.04
Move to the folder containing the file :
$ cd /home/username/Documents/test-folder
after making sure you are in the folder ccontaining the file try:
$ python file_name.py
if it doesn't work try the full path to python and the full path to the file like this:
$ /usr/lib/python3.7 /home/username/Documents/test-folder/file_name.py
If you want to use script.sh from anywhere, your approach won't work.
When executing $ bash script.sh, the file script.sh is expected to be located in the working directory.
You can use absolute paths to solve this:
# filename: script.sh
python3.7 /home/username/Documents/test-folder/demo.py
Try executing this with bash /home/username/Documents/test-folder/script.py.
You can achieve your goal in an arguably simpler way by adding /home/username/Documents/test-folder to your PATH, adding a shebang to demo.py and making demo.sh executable.
$ export PATH=/home/username/Documents/test-folder:$PATH
$ chmod +x /home/username/Documents/test-folder/demo.py
demo.sh will look like this with a shebang:
#!/usr/lib/python3.7
print('hello world')
and can be executed from anywhere with $ demo.sh.
It works for me. Is there anything about your setup that is different than mine?
$ cat demo.py
print("Hello World")
$ cat script.sh
python3.7 demo.py
$ tree
.
└── test-folder
├── demo.py
└── script.sh
1 directory, 2 files
$ cd test-folder/
$ bash script.sh
Hello World
Edit: Try the following.
$ cat script.sh
python3.7 "`dirname "$0"`/demo.py"
If your script.sh file is only there to run the Python script, it’s not needed. Instead make the Python script itself executable.
(1) add this as the first line in your Python file — this tells Unix which interpreter to use when running this file:
#!/usr/bin/env python37
(2) make your Python file executable so it can be run directly:
chmod +x demo.py
(3) put that file on the path, e.g. a path you set via a Bash config file, or one that’s already on the path like /usr/local/bin.
Now any time you give the command demo.py your script will run. Note the .py extension isn’t required — you can name the file anything you like.
I'm attempting to run a python script via nohup, and calling out the full path to it throws no such file or directory.
Examples:
nohup python3 ./script.py
Works
nohup python3 /full/path/to/file/script.py
Does not work.
cat nohup.out
Python: can't open file '/full/path/to/file/script.py':
[Errno 2] No such file or directory
Why can't nohup find the file if I give it the full path, but can if I'm in the directory?
It also works in an absolute path. Below is an example of it.
I am printing Linux server time through python script with both absolute an relative path.
[root#ip-172-31-11-214 tmp]# pwd
/tmp
[root#ip-172-31-11-214 tmp]# ls -lrth
total 4.0K
-rw-rw-r-- 1 ec2-user ec2-user 113 Aug 9 20:28 test.py
[root#ip-172-31-11-214 tmp]# nohup python test.py
nohup: ignoring input and appending output to ‘nohup.out’
[root#ip-172-31-11-214 tmp]# cat nohup.out
This line will be printed.
2019-08-09 20:29:37.417722
[root#ip-172-31-11-214 tmp]# cd /root/
[root#ip-172-31-11-214 ~]# pwd
/root
[root#ip-172-31-11-214 ~]# nohup python /tmp/test.py
nohup: ignoring input and appending output to ‘nohup.out’
[root#ip-172-31-11-214 ~]# cat /root/nohup.out
This line will be printed.
2019-08-09 20:30:26.057960
[root#ip-172-31-11-214 ~]#
I have a python 3 program in file foo the file has execution permissions, and the first line of the script is
#!/usr/bin/python3
When I run the file from python directly, i.e:
/usr/bin/python3 foo 3 boo
Everything runs perfectly well, but when I try to run the program by calling the file name I get:
foo 3 boo
foo: Command not found.
Even when specifying the relative or full path to the script I get the same response.
./foo 3 boo
./foo: Command not found.
/full/path/to/the/file/foo 3 boo
/full/path/to/the/file/foo: Command not found.
Some more info that was requested:
head -2 foo
#!/usr/bin/python3
which python3
/usr/bin/python3
Working from tcsh, where is the problem?
I'm working on a remote university computer through ssh, could it be the case that the sysadmins somehow prevent this?
You need to specify the absolute path of the script. Try
./foo 3 boo
while in the same directory as the script. Otherwise, the shell will only look in your PATH for something named foo, which doesn't exist.
You must give the path to the file
./foo
If you just call "foo", the shell will look in the environment variable $PATH the folder that contains the "foo" binary, and he will not find it ...
I'm using python3, just an example illustration..
Just a test script ..
$ cat test.py
#!/python/v3.6.1/bin/python3
import os
print(os.system("ls"))
$ which python3
/python/v3.6.1/bin/python3
Permission to make it executable.. i'm have doubt if the script foo is executable in your case..
$ chmod +x test.py
Test Run..
$ ./test.py
a.py Backup File_Write_Method NWK-old Python_aritsTest Python_Ftp Python_Mail Python_Primer test.py
argpass BASH Network_DeOps Python-3.6.3 Python_Dump Python_FTP Python_Panda readme_python tmp
awk.sh dnspython nslookup Python-3.6.3.tar.xz Python_excep Python_ldap Python_Parsers Regular_Expr tt.csv
0
It might be shell problem as i reproduced the tcsh shell & it fails to run..
$ tcsh
$ ./test
./test: Command not found.
but it runs , when i run it like below..
tcsh -c ./test.py
Assuming your script is executable and you have python3 in /usr/bin/python3. I think you are trying to run your script from a partition mounted with the noexec argument to check if it is the problem you should run:
mount | grep <partition> | grep noexec
If I'm right you can just mount the partition with the exec option or move your script elsewhere.
Massive apologies for this embarrassing question—
I'm using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are laughing at me:
Let me preface with:
$ whereis python
/usr/bin/python
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
(Is this my issue?)
I wrote helloworld.py to /users/charles in vim:
$ vim helloworld.py
#!/usr/bin/python
# Hello World Python Program
print "Hello World!";
When trying to run it from terminal:
$ helloworld.py
-bash: helloworld.py: command not found
When trying to run it from python:
$ python
>>> helloworld.py
Traceback (most recent call last):
File :<stdin>", line 1, in <module>
NameError: name 'helloworld' is not defined
From Dive Into Python (not sure if this is pertinent):
$ python
>>> import sys,os
>>> print 'sys.argv[0] =',sys.argv[0]
sys.argv[0]=
>>> pathname=os.path.dirname(sys.argv[0])
>>> print 'path=',pathname
path=
>>> print 'full path=',os.path.abspath(pathname)
full path= /Users/charles
I'm befuddled! Do I need to alter one of my paths so it finds my script?
I'm absolutely new to programming, I actually just found out that terminal was something you could use.
Thanks!
Let's start with the first error you received. Understanding error messages is important.
-bash: helloworld.py: command not found
This indicates that helloworld.py is not a command that can be executed. To run the file, you then have two options:
Run it using the python interpreter. python helloworld.py
Make the file executable and then run it directly. ./helloworld.py
To make files executable in a *nix environment, you have to change their mode to allow execution. In order to do this, you use the chmod command (man chmod for more info).
chmod +x helloworld.py
This assumes that you are in the directory containing the helloworld.py file. If not, cd there first or use the full path.
The ./ is necessary because it tells the shell to run the file located here, not by looking in $PATH. $PATH is a list of possible executable locations. When you try to run helloworld.py directly, the shell tries to look for it in $PATH. You want to run the local file, so you have to prefix it with ./, which means "from here".
As an aside, note the first line of your python script:
#!/usr/bin/python
This is called a shebang line and tells system to use the /usr/bin/python executable to load the file. Internally, that means that the program loader will be doing /user/bin/python helloworld.py.
Finally, when you called python with no arguments, you were dropped into an interactive Python interpreter session. >>> helloworld.py in this environment is not referencing the file of that name, it's just interpreted as python code. Invalid python code. This is why you get your second error, NameError: name 'helloworld' is not defined.
To turn a Python module or script into a standalone program on a UNIX system you have to do two things:
1.) Make sure you have the "shebang" in the top of your script:
#!/usr/bin/python
2.) Make sure the script file is executable. This is done using the chmod command:
chmod +x /path/to/helloworld.py
/path/to/ being the fully qualified file path to your script. If it's in the current directory, then you can omit the path.
% ls -l
total 0
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rw-r--r-- 1 jathan jathan 0 2011-04-13 15:28 helloworld.py
It's in my current directory, so let's make it executable!
% chmod +x helloworld.py
% ls -l
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rwxr-xr-x 1 jathan jathan 0 2011-04-13 15:28 helloworld.py*
See the "x"s in the permission bits on the left? You've done it! Now we can run it:
% ./helloworld.py
Hello World!
Lastly, never use semicolons as line-endings in Python. It's not required and it's ugly!
Wanted to add my 2 cents: Apart from permissions and path answers above, there is one more situation where you may still face the same error.
In-spite of correct permissions and the shebang header, you may still get the same "Command not found" error if you've originally written the file in Windows and copied it over to Linux. Due to differing line-ending characters, there will be extra '\r' characters on the lines.
This happens because there are non-printable characters in the file. Examing it by doing:
cat -v <filename>:
#!/usr/intel/bin/python^M
The extra "^M" is the problem. Use 'dos2unix' to convert the file and then it'll run fine.
as others said you should chmod +x your file to make it executable and if you don't want to put "./" in your coomand line you should add your current place as system path:
export PATH=$PATH:.
If you're already within python, the syntax to load your script is not helloworld.py :
import helloworld
or
from helloworld import *
you only use the extension .py when you're running python with a script as a command line argument.
No need to apologize, have to start somewhere, and the error messages can be cryptic when you're having basic syntax problems.
Make sure your terminal's current working directory is where your .py file is.
EDITED:
try doing /usr/bin/python helloworld.py on commmand line