Getting Python error "from: can't read /var/mail/Bio"
Asked Answered
P

7

119

I am running a (bio)python script which results in the following error:

from: can't read /var/mail/Bio

seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.

What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:

from Bio import SeqIO
from Bio.SeqUtils import ProtParam

handle = open("examplefasta.fasta") 
for record in SeqIO.parse(handle, "fasta"): 
    seq = str(record.seq)
    X = ProtParam.ProteinAnalysis(seq)
    print X.count_amino_acids() 
    print X.get_amino_acids_percent() 
    print X.molecular_weight() 
    print X.aromaticity() 
    print X.instability_index() 
    print X.flexibility() 
    print X.isoelectric_point() 
    print X.secondary_structure_fraction()

what is the problem here? bad python setup? I really don't think it's the script.

Periphrastic answered 17/4, 2013 at 20:37 Comment(0)
F
243

No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).

Another possibility is to add the following line to the top of the script:

#!/usr/bin/env python

This will instruct your shell to execute the script via python instead of trying to interpret it on its own.

Flashy answered 17/4, 2013 at 20:39 Comment(0)
D
18

I ran into a similar error when trying to run a command. After reading the answer by Tamás, I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).

Solution was to first enter in the Python shell with the command python and when you get these >>> then run any Python commands.

Dentate answered 23/7, 2015 at 12:5 Comment(0)
F
6

Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.

./manage.py shell

and only then

>>> from django.contrib.sites.models import Site
Foursome answered 24/9, 2016 at 11:38 Comment(0)
E
5

Put this at the top of your .py file (for Python 2.x)

#!/usr/bin/env python 

or for Python 3.x

#!/usr/bin/env python3

This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put

#!/#path/#to/#python
Exigent answered 11/7, 2018 at 6:56 Comment(1)
FWIW, specifying python3 might not be necessary these days on some systems now that Python 2 is EOL.Scone
C
2

#!Double/check/your/shebang/line
My case: It was OK with PyCham or Jupyter (∵ they don't refer to the shebang line), but when I ran the same snippet from the command line, it complained, can't read /var/mail/blah, and I was at my wit's end. What's wrong with # !/usr/bin/env python3??
I spent 90 mins fixing it; that's why I'm here. Finally, I found a space between # and ! and removed it. So now it's all right!

OK, but why /var/mail/?: With a broken (or no) shebang, you are executing a shell command from (Bio import seqIO).
In this situation, from is not a Python keyword but a utility command that prints out the mail header lines from the invoker's mailbox. For the details, try man from on the Terminal.

Coverup answered 26/4, 2023 at 11:46 Comment(1)
yes, this works exactly for me.Robrobaina
T
0

for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.

Texas answered 14/3, 2020 at 16:21 Comment(1)
What do those do exactly?Scone
M
0

For Flask users, before writing the commands, first make sure you enter the Flask shell using:

flask shell
Misrule answered 19/6, 2022 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.