Unable to use directory variable send to shell file to execute python script - python-3.x

So I'm trying the send a variable which I created, to a shell script I am calling within the parent script. The variable happens to be contain a directory which contains my python.exe. I'm receiving the variable all right, but when i try appending /python and executing a python script I'm getting "ModuleImportError", but when i use the hard coded path every thing works fine.
Parent script call startServiceConsumer
$PYTHON_HOME = /.conda/envs/mypika/bin
./startServiceConsumer.sh $PYTHON_HOME;
startServiceConsumer.sh
pythonHome=$1
echo $pythonHome
cd src/Controllers # Source project
$pythonHome/python xyz.py
exit
Error
Traceback (most recent call last):
File "xyz.py", line 3, in <module>
from src.Controllers import TaskProcessing

Related

Unable to run Porter5: generating `.flatpsi` file instead of `.psi`

I am trying to use Porter5 to run protein secondary structure prediction on a FASTA file containing a bunch of protein sequences. I am using a Linux machine.
For starters, I decided to try using the example file that gets downloaded along with Porter5, called 2FLGA.fasta. The command I used was the one I found on the GitHub page for Porter5 (https://github.com/mircare/Porter5/)
$ python3 Porter5.py -i example/2FLGA.fasta --cpu 4
I got the following error message:
sh: 1: /home/user/ncbi-blast-2.8.1+/bin/psiblast: not found
PSI-BLAST executed in 0.01s
wc: example/2FLGA.fasta.psi: No such file or directory
awk: cannot open example/2FLGA.fasta.psi (No such file or directory)
HHblits executed in 0.01s
Traceback (most recent call last):
File "/home/user/Porter5/scripts/process-alignment.py", line 37, in <module>
sequences = lines[0] = len(lines) - 1
IndexError: list assignment index out of range
Traceback (most recent call last):
File "Porter5.py", line 80, in <module>
flatpsi_ann = open(filename+".flatpsi.ann", "r").readlines()
FileNotFoundError: [Errno 2] No such file or directory: 'example/2FLGA.fasta.flatpsi.ann'
After PSI-BLAST, the Porter5 script is expecting an output file called 2FLGA.fasta.psi. I checked the example directory and it contains an output file called 2FLGA.fasta.flatpsi.
I'm not sure what to do here. I don't want to try modifying any of the Porter5 scripts to look for .flatpsi files instead of .psi files because I am a beginner at programming, and I don't want all hell to break loose by tampering with the code.
Could someone please help me with this? Any help is appreciated.
(There are a bunch of errors to negotiate with later but I'll see about those after dealing with the first one.)
I am the author of Porter5 and I generally recommend to open an issue straight on GitHub since I don't get any notification otherwise.
It looks like the path of psiblast is wrong (first line of your error message). You can check that with the following command:
$ ls /home/user/ncbi-blast-2.8.1+/bin/psiblast
Also, the path for the executable or the database of HHblits is wrong, or maybe both. You can check that as follow (within Porter5/):
$ cat scripts/config.ini
You can either edit scripts/config.ini or run the following command until Porter5 runs succesfully:
$ python3 Porter5.py -i example/2FLGA.fasta --cpu 4 --setup
(The .flatpsi is an intermediate file, it doesn't contain a valid representation if HHblits doesn't run succesfully)

Timeout using python subprocess

I've got a very strange error. I load large amounts of data to a PG database using psql. One function in my code does ALL loads. One part of my codebase calls the load function, and it works JUST fine. Another part calls the same function with different data), and the psql subprocess call hangs (timeout has to kill it). Executing the SAME command from the command line works just fine:
Here is my code:
myEnv = os.environ.copy()
myEnv["PGPASSWORD"] = <<db password>>
output = None
output = subprocess.check_output(popenArgs, stderr=subprocess.STDOUT, timeout=120, env=myEnv)
The timeout error is:
Traceback (most recent call last):
File "C:\Data\Dropbox\Engagements\<Client>\Src\prod_db.py", line 102, in _copyFrom
output = subprocess.check_output(popenArgs, stderr=subprocess.STDOUT, timeout=120, env=myEnv)
File "C:\Tools\WinPython-64bit-3.5.1.3\python-3.5.1.amd64\lib\subprocess.py", line 629, in check_output
**kwargs).stdout
File "C:\Tools\WinPython-64bit-3.5.1.3\python-3.5.1.amd64\lib\subprocess.py", line 703, in run
stderr=stderr)
subprocess.TimeoutExpired: Command '"C:/Program Files/PostgreSQL/9.4/bin/psql.exe" -h <<DB LOCATION>> -p 5432 -d forecast_dev -U forecast -v ON_ERROR_STOP=1 -AtXwa -c "\copy di_entities_load from C:\Users\Marc\AppData\Local\Temp\copytempdi_entities_load7.csv with csv"' timed out after 120 seconds
If I set the PGPASSWORD variable manually in a command line window, and then copy and paste the command in the error to the command line, it runs fine, and quickly, exiting and returning upon completion.
As I said, this code works when called from a different part of my application, to a different table, with different data.
Any idea what would cause this to fail when called from python, while it still works on the command line?
Windows 10 Pro, Py3.5, PG 9.4, pg8000 db module.
So, it turns out that I was locking the table prior to the copy, and since the copy runs in a different process, it was seeing a locked table to write to and hanging.

Python3: configparser KeyError when run as Cronjob

i have a simple python3 Script that works when I'm running it from console:
import configparser
file = 'config_test.ini'
config = configparser.ConfigParser()
config.read(file)
for key in config['test_section']: print(key)
the called ini-file looks like this:
[test_section]
testkey1 = 5
testkey2 = 42878180
testkey3 = WR50MS10:1100012307
testkey4 = WR50MS04:1100012010
testkex5 = 192.168.200.168
and the script runs fine and returns the five keys of the ini-file.
No I configured it as a cronjob every minute (running on rasbian on Raspberry Pi) via:
* * * * * python3 /home/pi/s0/testconfig.py >> /tmp/cron_debug_log.log 2>&1
and the log looks like this:
Traceback (most recent call last):
File "/home/pi/s0/testconfig.py", line 7, in <module>
for key in config['test_section']: print(key)
File "/usr/lib/python3.2/configparser.py", line 941, in __getitem__
raise KeyError(key)
KeyError: 'test_section'
Does anyone have an idea what I did wrong
Kind regards
When running from cron, your script runs in a different working directory. This means the filename it is trying to open is relative to a different directory.
You should use the absolute path to the config file in your script.
A common way of doing it in python, assuming the config file resides in the same directory as the script you're running, is using the __file__ builtin:
config_file = os.path.join(os.path.dirname(__file__), 'config_test.ini')

Execute command on linux terminal using subprocess in python

I want to execute following command on linux terminal using python script
hg log -r "((last(tag())):(first(last(tag(),2))))" work
This command give changesets between last two tags who have affected files in "work" directory
I tried:
import subprocess
releaseNotesFile = 'diff.txt'
with open(releaseNotesFile, 'w') as f:
f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))
error:
abort: unknown revision '((last(tag())):(first(last(tag(),2))))'!
Traceback (most recent call last):
File "test.py", line 4, in <module>
f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))
TypeError: expected a character buffer object
Working with os.popen()
with open(releaseNotesFile, 'w') as file:
f = os.popen('hg log -r "((last(tag())):(first(last(tag(),2))))" work')
file.write(f.read())
How to execute that command using subprocess ?
To solve your problem, change the f.write(subprocess... line to:
f.write(subprocess.call(['hg', 'log', '-r', '((last(tag())):(first(last(tag(),2))))', 'dcpp']))
Explanation
When calling a program from a command line (like bash), will "ignore" the " characters. The two commands below are equivalent:
hg log -r something
hg "log" "-r" "something"
In your specific case, the original version in the shell has to be enclosed in double quotes because it has parenthesis and those have a special meaning in bash. In python that is not necessary since you are enclosing them using single quotes.

Subprocess library won't execute compgen

I am trying to make list of every command available on my linux (Lubuntu) machine. I would like to further work with that list in Python. Normally to list the commands in the console I would write "compgen -c" and it would print the results to stdout.
I would like to execute that command using Python subprocess library but it gives me an error and I don't know why.
Here is the code:
#!/usr/bin/python
import subprocess
#get list of available linux commands
l_commands = subprocess.Popen(['compgen', '-c'])
print l_commands
Here is the error I'm getting:
Traceback (most recent call last):
File "commands.py", line 6, in <module>
l_commands = subprocess.Popen(['compgen', '-c'])
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I'm stuck. Could you guys help me with this? How to I execute the compgen command using subprocess?
compgen is a builtin bash command, run it in the shell:
from subprocess import check_output
output = check_output('compgen -c', shell=True, executable='/bin/bash')
commands = output.splitlines()
You could also write it as:
output = check_output(['/bin/bash', '-c', 'compgen -c'])
But it puts the essential part (compgen) last, so I prefer the first variant.
I'm not sure what compgen is, but that path needs to be absolute. When I use subprocess, I spell out the exact page /absolute/path/to/compgen

Resources