I have a c++ code that is compiled and can be executed.
Let's say the output file after compiling is executable.x.
I also have python script that can call this executable and run it.
pythonScript.py:
# lets say the path is absolute for simplicity.
file_path = 'C:/MyProject/code/executable.x'
# I need to pass an argument to main.cpp
subprocess.check_call([file_path, '-switch1'])
I can run the python script from terminal, and it runs the executable without any issue.
Then there is a shell command to run the python script.
myShell.sh
#!/bin/sh
pwd
(cd pythonScriptDirectory && python3 pythonScript.py)
pwd
By running the sh script, it sets the working directory (like how I run python script from terminal), and then it runs the python script. It seems it also finds the executable.x, but it always return with some error.
Is there any suggestion what might be wrong here, or what would be the debugging approach.
The return value specifies the error code 3221225785 in decimal, which is C000 0139 Hex. My assumption is that the executable file can be selected to run, but a working directory issue causes that libraries being used by executable cannot be loaded.
Here is directory structure:
Related
Let's say I have two files we'll call test1.py and test2.py, and I want to run both of these files as executables. I'm familiar with the standard procedure of adding a shebang followed by the path to the desired python interpreter and then running chmod u="rwx" file.py.
I also know that when using conda, each environment gets its own unique interpreter with which to run scripts. So naturally, I activate my environment, run which python and add that command's output to my script like so...
test1.py
#!/home/my_name/anaconda3/envs/env_name/bin/python
print("foo")
Which when I run it as ./test1.py gives me the following error...
./test1.py: line 2: syntax error near unexpected token `"foo"'
./test1.py: line 2: `print("foo")'
However simply running python test1.py gives...
foo
Now let's say I return to my base environment and following the same procedure as above, I create the following script...
test2.py
#!/home/my_name/anaconda3/bin/python
print("foo")
This script runs without error and gives the correct output regardless of whether or not I run it as an executable.
What do I need to do in order to run my python scripts without these errors?
EDIT
Running which python in env_name gives
/home/my_name/anaconda3/envs/env_name/bin/python
Whereas running the same command in base gives
/home/my_name/anaconda3/bin/python
I had the same issue when line 1 was empty, and the interpreter was set in line 2. This results in bash assuming that it's a bash script, and as a result, you get a "syntax error" from trying to execute python commands in bash.
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'])
I am trying to run the Example Workflow in https://rki_bioinformatics.gitlab.io/ditasic/, in which example.sh is the major bash script that will take the example data and output some data matrices.
In the example.sh script which will run the example workflow, we have the following line 9:
ditasic_matrix.py -l 100 -o output/similarity_matrix35.npy data/reference_paths
However, when example.sh is run in the terminal of macOS, the following message arises:
DiTaSic /ditasic_example/example.sh: line 9: ditasic_matrix.py: command not found
But ditasic_matrix.py already exists in the path I have set for the terminal. I have put ditasic_matrix.py in a directory whose path I have added to the PATH of the terminal by
export PATH="$PATH":
So what has happened that leads to the command not found?
Change ditasic_matrix.py line in your script to be ./ditasic_matrix.py because of current path not being included in executable search.
If it still doesn't execute, maybe the file does not have executable bit set.
Open a terminal/console in that folder and issue
chmod +x ditasic_matrix.py
The ditasic_matrix.py file seems to have the following interpreter setting on it: #!/usr/bin/env python. Since you seem to not be able to run it, it seems that this is not your actual path to running Python. Please make sure that:
1) You have Python installed
2) You can execute Python programs by running python in the command line.
I setup Ubuntu server 18.04 LTS, LAMP, and mod_mono (which appears to be working fine alongside PHP now by the way.) Got python working too; at first it gave an HTTP "Internal Server Error" message. sudo chmod +x myfile.py fixed this error and the code python generates is displayed fine. But any time the execute permission is removed from the file (such as by uploading a new version of the file), the execute bit is stripped and it breaks again.
A work-around was implemented with incrontab, where the cgi-bin folder was monitored for changes and any new writes caused chmod +x %f to be ran on them. This worked for awhile, then stopped, and seems a hokey solution at best. Perl, PHP, even ASPX do not need to be marked executable - only python.
Is there any way Apache can "run" python without the file marked as executable?
The reason PHP works, is because the interpreter is loaded into Apache. So Apache interprets the code.
For your Python, it runs as a CGI, so the interpreter is outside of Apache.
In your Python script, you probably have a #!/usr/bin/python first line (or something similar). This tells the script to run using this interpreter. This requires executable permission on the .py file, and allows you to call myfile.py directectly.
Instead run it like this: /usr/bin/python myfile.py. This way the interpreter is the executable, and it will run myfile.py as the code.
Examples
You want to run the py file "alone":
file.py
#!/usr/bin/python
print("Hello")
Running it:
./file.py
You want to run it via the python executable, like you want via Apache:
file.py
print("Hello")
Running it:
/usr/bin/python file.py
I don't think Apache is capable of serving executed python scripts without the execute bit set on the .py file.
But here is a work-around: simply leave that file marked executable, but import a second python file. That second file does not need to be marked executable.
myfile.py (marked as executable and read-only - use this with apache):
#!/usr/bin/python3
# enable debugging
# helper to run the other, non-executable file
# do not add .py to the import "filename"
import myfile2
myfile2.py (marked RW only, edit this file freely):
# this is the code which can change frequently
# and does not need to be marked executable...
print("Content-type: text/html\n\n")
print("<html><head><title>Python</title></head>")
print("<body>Hello, World!</body></html>")
So, I am trying to run a Shell script from Python and I double checked that the location of the script.sh is all correct (because when I run it from sublime, the script.sh opens). What I have to call script.sh is:
subprocess.call("script.sh", shell=True)
When I run that, the function returns 0. However, the script is supposed to create a file in my folder and write into it, which it is not doing. It does work when I run the script from cygwin command prompt.
What could be wrong?
Please ensure you have added:
!/bin/bash
as the first line and also make sure that the file script.sh has executable permission.
chmod u+x script.sh
then try specifying the complete path:
subprocess.call("/complete/path/script.sh", shell=True)
At the top of your shell script somewhere, put the line:
pwd >/tmp/mytempfile
and then run the Python script and go look into that file.
This will let you find out the working directory of your scripts which, in the majority of cases where a file doesn't appear to be created, is different to what you think it should be.
You may also want to check the shell script to ensure it's not changing the working directory before creating the file but that would be unlikely given you've stated it works okay from the command line.
If you add the line to create the temporary file and it doesn't actually get created, then your script is not executing.
In that case, you could try a few things. The first is to fully specify the script on the off chance that Python isn't in the correct directory. In other words, something like:
subprocess.call("/actual/path/to/script.sh", shell=True)
Or you could try to run the actual bash executable with the script as an argument:
subprocess.call("bash -c script.sh", shell=True)