I had a small python script that takes input from the command line arguments and done some
operations using the inputs taken and display the result
Below is the working example code
some_file.py
import sys
arguments = sys.argv
first_name = sys.argv[1]
second_name = sys.argv[2]
print "Hello {0} {1} !!!!".format(first_name,second_name)
Now i am executing this as
python some_file.py Steve jobs
Result :
Hello Steve Jobs !!!!
Now what all i want is, i don't want to use python command before file name and extension of python file name, that is i want to run the file as a command tool as below
some_file Steve Jobs
so what to do in order to run the python file as above ?
Unix-like OS solution: the first line of the file should be #!/usr/bin/python (or wherever the python interpreter is) and chmod u+x the script. Run it with ./some_file parameters.
If you want to launch it with some_file parameters simply make a link to the script in a directory which is already included into your PATH: sudo ln -s some_file /usr/bin.
So, here's the full procedure:
blackbear#blackbear-laptop:~$ cat > hw
#!/usr/bin/python
print "Hello World!"
blackbear#blackbear-laptop:~$ chmod u+x hw
blackbear#blackbear-laptop:~$ sudo ln -s hw /usr/bin
blackbear#blackbear-laptop:~$ hw
Hello World!
blackbear#blackbear-laptop:~$
make a symbolic link
ln -s some_file.py some_file
now you can type your cmd like this:
some_file Steve Jobs
you can run the same program in python shell by using execfile('path').
Related
Cron doesn't compile my .py file and I can't get an e-mail from Cron to understand why Cron doesn't work. But if I just run the command ~/t/test.sh in terminal everything works fine.
Code in the cron:
SHELL=/bin/bash
MAILTO=mymail#gmail.com
30 0-23 1-31 1-12 0-6 ~/t/test.sh
I ty to run a script test.sh which has the code:
#!/bin/bash
cd /home/alex/t && python3.8 ./test.py
Code in the test.py:
from datetime import datetime
current_time = datetime.now()
f = open("text.txt", 'w+')
f.write("Hello world! Now is {0}\n".format(current_time))
f.close()
I Launched Cron via the sudo crontab-e and crontab-e commands. I Put Cron in the first line MAILTO=mymail#gmail.com.
I Installed yum install mailx. I tried adding the command >/dev/null 2>&1 to Cron, i.e. writing
* * * * * ~/t/test.sh >/dev/null 2>&1
Nothing helps
I Use Ubuntu 18.04
Python3.8.2
Here is what the logs for the grep CRON /var/log/syslog command show:
Apr 30 15:35:01 av CRON[2130]: (alex) CMD (~/t/test.sh)
Apr 30 15:35:01 av CRON[2129]: (alex) MAIL (mailer 68 bytes of output put get status 0x004b from MTA#012)
Has anyone encountered this problem?
Can you help?
your test.sh should be something like this: (needs full path)
#!/bin/bash
/"location where python is installed"/python/bin/python /home/alex/t/test.py
The weakest link in you code is the fact that you invoke the Python 3.8 interpreter without a full path. You invoke bash using a full absolute path, cd to an absolute path, and access your script through a path to the directory into which you cd-ed. Only Python is assuming the existence of some directories in the PATH environment variable.
Try accessing Python 3.8 through the full path to its location.
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 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.
Laptop A has python file "file1.py". Computer B has python file "file2.py". I want to remotely enter into Computer B and run the python script file2.py. I am using SCREEN, and below is my code.
import os
import time
os.system('screen -S Test -d -m /dev/ttyUSB0 57600')
time.sleep(1)
os.system('screen -S Test -X stuff "file2.py"')
time.sleep(1)
os.system('screen -S Test -d -r')
time.sleep(0.25)
print "done"
How did you try to run your 'remote' script?
You can try ssh session:
ssh user#computer_b 'python file2.py'
Of course, you should provide a full path to your file2.py script and maybe (I'm not 100% sure) a full path to a Python executable on your remote Computer B. Another option is to make your file2.py executable, by adding a Python "shebang line" as the first line of your file2.py script and setting executable bit via chmod +x file2.py:
Should I put #! (shebang) in Python scripts, and what form should it take?
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.