Need a solution to execute python code, ".py", in the sh environment - python

I would like to execute a circleCN.py python script by using #!/bin/sh.
I have searched the internet, found some suggestions, and none work. I get permission denied, and I changed my chmod of circleCN.py with a+x, I get invalid syntax, or I get another error. I have tried . /path/circleCN.py, exec /path/circleCN.py, python -c "/path/circleCN.py" and none work.
I have also tried to change #!/bin/sh to #!/usr/bin/env python and had no success. I would like to keep #!/bin/sh though.
I am very new to shell programming. I am also new to Linux.
When OpenFOAM users write Allrun or Allclean scripts, they use the #!/bin/sh shebang. I am working on an automation script and I would like to conform to the standard.

Why do you need to execute python script under a bash shebang?
You can invoke python script inside the shell script like this
#!/bin/sh
python /path_to_file/circleCN.py

Related

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])

Running a python script via Powershell script

I have a python script which I can run via PowerShell using the following code:
cd User\PythonScripts
python TestFile.py
Now I want to run these simple commands via a PowerShell script (notepad file and saving it as a ps1 file).
I have googled a lot but I cannot find an answer, but I think it should be something like this:
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py
I think I still miss the reference to python (so it knows which program he needs). How do I need to do this?
Assuming that python is already in your path variables you can just call a python script like this:
python C:\User\PythonScripts\TestFile.py
I think the question is you want to run the python script using powershell .
I think below code will do for you
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py'
$cmd = $path+"\\"+$file # This line of code will create the concatenate the path and file
Start-Process $cmd # This line will execute the cmd
Save the above code as .ps1 and run the that powershell file
I have been facing the same problem with Windows10 PowerShell and I have solved it like this:
Go to PowerShell and instead of running the command like this:
PS C:\Windows\system32> python ex1.py
run it as below:
PS C:\Windows\system32> python C:\Users\PCWIZARD\Desktop\hardway\ex1.py
In other words specify the entire path of your ex1.py depending on where you saved your file on your computer. This will print out your code.
I think below code will work.
cd C:\User\PythonScripts
.\python TestFile.py
Mention python.exe if it is a executable in place of python.
Save above code in .ps1 file and run it on powershell

How to run a python script inside Applescript?

I had to do an Applescript and by the end of it i just want to run a python script.
I wrote
do shell script "/Users/Tom/Desktop/ayscript.py"
But it's said "permission denied"
Any idea ?
You are trying to run the script directly instead of using Python to run it. You need to do shell script python, passing the path to your script as an argument.
do shell script "python /Users/Tom/Desktop/ayscript.py"

Permission denied for Python script using Bash?

I am new to Ubuntu... I am trying to run my first simple python program "Hello World" ...
After running following commands in terminal
1. chmod +x filename.py
2. ./filename.py
terminal is showing following error "bash: ./filename.py: Permission denied"
what can I do for solve about problem?
Do you have the appropriate incantation at the top of your python file? e.g.,
#!/usr/bin/python (or alternatively #!/usr/bin/env python)
Just to clarify, chmod +x only makes a file executable, it doesn't run it.
And I'm assuming your script looks like nothing more complex than this:
#!/usr/bin/env python
print 'hello world'
Some possibilities:
What does it say if you type umask? chmod +x will only make a file executable for you if your umask doesn't block the user executable bit. A typical umask such as 0022 will not block the user execute bit, but a umask like 0122 can. (See the Description section of chmod(1) for more info.)
To execute a script such as a Python script, you also need read permission. Try chmod u+rx filename.py and execute the script again.
It's also remotely possible that whatever interpreter you've specified in the file with the "hashbang" line at the beginning of your file (e.g. #!/usr/bin/env python) isn't executable, although that in my experience yields a different error message.
I deal with the same problem on my new system.
It is the third time I tried to solve this, and your post is the first one appearing on google results. My post is late, but think that it will help another users with the same problem.
In my case, it was about partition table setup.
Check in your /etc/mtab file how python script is being stored. Check if there is a clause: noexec
noexec is a flag that forbid executing under the partition. By default, it is set with exec. But, sometimes, this kind of things happen.
Now, it is working fine here.

How do I call a Python/Perl script in bin folder from a Bash script?

I previously used to copy Python/Perl scripts to access from my bash script. Duplication is not a good idea I know! Is there a way to call them from bin or libs folder that we have set up?
For instance :
My python script resides in /home/ThinkCode/libs/python/script.py
My bash script resides in /home/ThinkCode/NewProcess/ProjectA/run.sh
Now I want to use/call script.py from run.sh
Thank you!
Make this the first line of your python script (bash will then know this is a python script and it should be run with python):
#/usr/bin/env python
EDIT: my bad, it should be #!/usr/bin/env python not #!/usr/bin/python. It is better to do it this way.
Then chmod your script with u+x (if not a+x).
Now your python script works as an executable. Your bash script, then, can call it like you'd call any executable.
Just do python /path/to/my/python/script.py.
In a bash script, you execute programs the same way you do from the bash command prompt.
/home/ThinkCode/libs/python/script.py
If this doesn't launch the script directly, you may need to add python to the beginning (like this: python /home/ThinkCode/libs/python/script.py) and/or ensure that the script is executable (with chmod +x /home/ThinkCode/libs/python/script.py).

Categories