I have the following file:
$ cat my_exec.sh
#!/usr/bin/env python
print(10)
It should just print 10. However, I can't get it to do so:
$ sudo ./my_exec.sh
sudo: ./my_exec.sh: command not found
$ sh my_exec.sh
my_exec.sh: line 3: syntax error near unexpected token `10'
my_exec.sh: line 3: `print(10)'
How do I run my file?
You can run it via the python command:
$ python my_exec.sh
To run it as simply ./my_exec.sh, you need to make the file executable first:
$ chmod 755 my_exec.sh
Also note that by convention python files end in .py .
Change the shebang to #!/usr/bin/env python
Change the filename to my_exec.py, as is convention for python files
You can run with python my_exec.py
You can chmod +x my_exec.py and then ./my_exec.py
You must enter the directory that you have saved you file through the cmd with cd command. After that you just execute the file with : python name_of_the_file.py . But first you must make it executable with chmod command
For example if you have saved your file at Desktop with the name mycode.py :
cd Desktop
chmod +x mycode.py
python mycode.py
Related
Try to create a Windows .bat file to achieve the below function:
cd C:\repo\demo
venv\Scripts\activate
python test.py
In Visual Studio Code terminal window, I can run the above lines without issue.
Created a .bat file as below:
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "python test.py"
pause
When double click the above .bat file to run it, end with error:
if [ "${BASH_SOURCE-}" = "$0" ]; then
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Also tried the below .bat code, not working either:
cd C:\repo\demo
venv\Scripts\activate
python test.py
pause
How to correct the .bat file to make it work?
======================================
Based on #Compo's comment, tried the below version and it successfully executed python test.py:
cd C:\repo\demo
call "venv\Scripts\activate.bat"
python test.py
pause
but seems it didn't finish call "venv\Scripts\activate.bat", the command line window shows as below:
When manually run the code, it will prefix the path with (venv) as below which shows the proper result:
============================================
UPDATE:
The below .bat version works now, an answer from this question
cd C:\repo\demo && call "venv\Scripts\activate.bat" && python test.py && pause
You should either remove "C:\...\python.exe" from the second line:
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
python heatmap.py <-- like this
pause
or remove python
cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "heatmap.py"
pause
Try this:
cd c:\python
python scripts/test.py <-- here, you can replace "test" with the name of the Python program(script) you want to execute you
I have a python file in: '/home/username/scripts/pyscript' and I want set a word for execute directly this script.
I want do this "python3 /home/username/scripts/pyscript/main.py arg1 arg2" but looks like
this "myscript arg1 arg2"
Is this posible?
Thank you anyway.
It is possibile in a number of ways. Links are for Bash, supposedly your shell but the ideas always apply.
First option: make a shell alias
alias myscript='python3 /home/username/scripts/pyscript/main.py'
Be sure to add the alias to your .profile to make it survive logout.
Second option: define a wrapper script. Create a file with the following content, named after your desired command (e.g. myscript):
#!/bin/bash
python3 /home/username/scripts/pyscript/main.py "$#"
save it and make it executable, then call it :
chmod +x myscript
./myscript arg1 arg2
Be sure to copy the script in a folder in your PATH (check where with echo $PATH) to be able to call it from any folder.
You can also use pyinstaller to create a single file executable:
Step 1: Install pyinstaller
[Note: best practice is to do this in a virutalenv]
$ pip install pyinstaller
Step 2: Run pyinstaller against your script
$ pyinstaller --console --onefile /home/username/scripts/pyscript
$ pyinstaller pyscript.spec # use this after the first run
Step 3: Test the generated executable
$ cd /home/username/scripts/dist # generated by pyinstaller
$ pyscript arg1 arg2
Step 4: Leverage the $PATH variable
$ cp /home/username/scripts/dist/pyscript /usr/bin
You should now be able to run the executable from anywhere.
It should be noted that the executable that is generated is OS specific. For example, if you generate it on an Ubuntu machine, it will only run on Ubuntu (Debian based). The same holds true for Windows and other Linux distros.
Finally I solver with the help of #pierpaciugo
I add a alias at the end of the .bashrc for make it persistent:
alias create='bash /home/username/Programming/Python/GithubAPI/script.sh'
I couldn't use only alias because I have my python dependencies on a virtual environment so if I try this i could not add params to my python script.
For that I create this bash script:
#!/bin/bash
source /home/username/Programming/Python/GithubAPI/venv/bin/activate && python3 /home/username/Programming/Python/GithubAPI/main.py $# && deactivate
Now I can write "create param1 param2" and it works.
I am using all global paths but could be a good idea add the script in a folder in my PATH.
I am trying to pipe output from a command written in the terminal to a Python script.
For example:
ls | ./foo.py
I wrote a Python script to do the same:
#foo.py
import fileinput
with fileinput.input() as f_input :
for line in f_input :
print(line,end='')
But this does not seem to work,
when I run the following command:
$ ls | sudo ./foo.py
I get an error that says:
$ ./foo.py: command not found
I have checked the working directory and I can see the foo.py when I use the ls command, so what am I doing wrong here?
It seems like you forgot the Shebang:
#!/usr/bin/env python3
import fileinput
with fileinput.input() as f_input :
for line in f_input :
print(line,end='')
Also remember make it as executable via command:
chmod +x foo.py
Then run your command again.
You have to pipe it to the Python executable, not to the name of a file. As the error says, that filename doesn't represent a command it knows.
ls | py ./foo.py
Use py or python or however you run the Python interpreter on your particular system.
I have a python script let's name it script1.py. I can run it in the terminal this way:
python /path/script1.py
...
but I want to run like a command-line program:
arbitraryname
...
how can i do it ?
You use a shebang line at the start of your script:
#!/usr/bin/env python
make the file executable:
chmod +x arbitraryname
and put it in a directory on your PATH (can be a symlink):
cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname
There are three parts:
Add a 'shebang' at the top of your script which tells how to execute your script
Give the script 'run' permissions.
Make the script in your PATH so you can run it from anywhere.
Adding a shebang
You need to add a shebang at the top of your script so the shell knows which interpreter to use when parsing your script. It is generally:
#!path/to/interpretter
To find the path to your python interpretter on your machine you can run the command:
which python
This will search your PATH to find the location of your python executable. It should come back with a absolute path which you can then use to form your shebang. Make sure your shebang is at the top of your python script:
#!/usr/bin/python
Run Permissions
You have to mark your script with run permissions so that your shell knows you want to actually execute it when you try to use it as a command. To do this you can run this command:
chmod +x myscript.py
Add the script to your path
The PATH environment variable is an ordered list of directories that your shell will search when looking for a command you are trying to run. So if you want your python script to be a command you can run from anywhere then it needs to be in your PATH. You can see the contents of your path running the command:
echo $PATH
This will print out a long line of text, where each directory is seperated by a semicolon. Whenever you are wondering where the actual location of an executable that you are running from your PATH, you can find it by running the command:
which <commandname>
Now you have two options: Add your script to a directory already in your PATH, or add a new directory to your PATH. I usually create a directory in my user home directory and then add it the PATH. To add things to your path you can run the command:
export PATH=/my/directory/with/pythonscript:$PATH
Now you should be able to run your python script as a command anywhere. BUT! if you close the shell window and open a new one, the new one won't remember the change you just made to your PATH. So if you want this change to be saved then you need to add that command at the bottom of your .bashrc or .bash_profile
Add the following line to the beginning script1.py
#!/usr/bin/env python
and then make the script executable:
$ chmod +x script1.py
If the script resides in a directory that appears in your PATH variable, you can simply type
$ script1.py
Otherwise, you'll need to provide the full path (either absolute or relative). This includes the current working directory, which should not be in your PATH.
$ ./script1.py
You need to use a hashbang. Add it to the first line of your python script.
#! <full path of python interpreter>
Then change the file permissions, and add the executing permission.
chmod +x <filename>
And finally execute it using
./<filename>
If its in the current directory,
I've tried googling the answer but with no luck.
I need to use my works supercomputer server, but for my python script to run, it must be executed via a shell script.
For example I want job.sh to execute python_script.py
How can this be accomplished?
Just make sure the python executable is in your PATH environment variable then add in your script
python path/to/the/python_script.py
Details:
In the file job.sh, put this
#!/bin/sh
python python_script.py
Execute this command to make the script runnable for you : chmod u+x job.sh
Run it : ./job.sh
Method 1 - Create a shell script:
Suppose you have a python file hello.py
Create a file called job.sh that contains
#!/bin/bash
python hello.py
mark it executable using
$ chmod +x job.sh
then run it
$ ./job.sh
Method 2 (BETTER) - Make the python itself run from shell:
Modify your script hello.py and add this as the first line
#!/usr/bin/env python
mark it executable using
$ chmod +x hello.py
then run it
$ ./hello.py
Save the following program as print.py:
#!/usr/bin/python3
print('Hello World')
Then in the terminal type:
chmod +x print.py
./print.py
You should be able to invoke it as python scriptname.py e.g.
# !/bin/bash
python /home/user/scriptname.py
Also make sure the script has permissions to run.
You can make it executable by using chmod u+x scriptname.py.
Imho, writing
python /path/to/script.py
Is quite wrong, especially in these days. Which python? python2.6? 2.7? 3.0? 3.1? Most of times you need to specify the python version in shebang tag of python file. I encourage to use #!/usr/bin/env python2 #or python2.6 or python3 or even python3.1 for compatibility.
In such case, is much better to have the script executable and invoke it directly:
#!/bin/bash
/path/to/script.py
This way the version of python you need is only written in one file. Most of system these days are having python2 and python3 in the meantime, and it happens that the symlink python points to python3, while most people expect it pointing to python2.
This works for me:
Create a new shell file job. So let's say:
touch job.sh and add command to run python script (you can even add command line arguments to that python, I usually predefine my command line arguments).
chmod +x job.sh
Inside job.sh add the following py files, let's say:
python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file.py"
python_file1.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file1.py"
Output of job.sh should look like this:
Done with python_file.py
Done with python_file1.py
I use this usually when I have to run multiple python files with different arguments, pre defined.
Note: Just a quick heads up on what's going on here:
python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "completed with python_file.py" .
Here shell script will run the file python_file.py and add multiple command-line arguments at run time to the python file.
This does not necessarily means, you have to pass command line arguments as well.
You can just use it like: python python_file.py, plain and simple.
Next up, the >> will print and store the output of this .py file in the testpy-output.txt file.
&& is a logical operator that will run only after the above is executed successfully and as an optional echo "completed with python_file.py" will be echoed on to your cli/terminal at run time.
This works best for me:
Add this at the top of the script:
#!c:/Python27/python.exe
(C:\Python27\python.exe is the path to the python.exe on my machine)
Then run the script via:
chmod +x script-name.py && script-name.py
I use this and it works fine
#/bin/bash
/usr/bin/python python python_script.py
Since the other posts say everything (and I stumbled upon this post while looking for the following).
Here is a way how to execute a python script from another python script:
Python 2:
execfile("somefile.py", global_vars, local_vars)
Python 3:
with open("somefile.py") as f:
code = compile(f.read(), "somefile.py", 'exec')
exec(code, global_vars, local_vars)
and you can supply args by providing some other sys.argv
Here I have demonstrated an example to run python script within a shell script. For different purposes you may need to read the output from a shell command, execute both python script and shell command within the same file.
To execute a shell command from python use os.system() method. To read output from a shell command use os.popen().
Following is an example which will grep all processes having the text sample_program.py inside of it. Then after collecting the process IDs (using python) it will kill them all.
#!/usr/bin/python3
import os
# listing all matched processes and taking the output into a variable s
s = os.popen("ps aux | grep 'sample_program.py'").read()
s = '\n'.join([l for l in s.split('\n') if "grep" not in l]) # avoiding killing the grep itself
print("To be killed:")
print(s)
# now manipulating this string s and finding the process IDs and killing them
os.system("kill -9 " + ' '.join([x.split()[1] for x in s.split('\n') if x]))
References:
Execute a python program from within a shell script
Assign output of os.system to a variable and prevent it from being displayed on the screen
If you have a bash script and you need to run inside of it a python3 script (with external modules), I recommend that you point in your bash script to your python path like this.
#!/usr/bin/env bash
-- bash code --
/usr/bin/python3 your_python.py
-- bash code --