pysam.AlignmentFile not working...

382 views
Skip to first unread message

Matthew Cserhati

unread,
Jun 14, 2021, 12:17:13 PM6/14/21
to Pysam User group
Hello all,

I keep getting this error:

    samfile = pysam.AlignmentFile(infile,"rb")
AttributeError: 'module' object has no attribute 'AlignmentFile'

I've tried conda, pip, manual github install but no luck. My pip version is 0.16.

Thanks, Matthew

Marcel Martin

unread,
Jun 14, 2021, 1:44:01 PM6/14/21
to pysam-us...@googlegroups.com
One reason for this could be that you have a leftover or broken pysam
installation in the user site-packages directory
(~/.local/lib/pythonX.Y/site-packages/), which can happen if you have
previously used 'pip install --user' to install pysam.

To debug this, try to find out from where the module is actually imported:

python3 -c 'import pysam; print(pysam.__file__)'

You may want to delete or rename the ~/.local/lib/python* directories.
Alternatively, you can do "export PYTHONNOUSERSITE=1" to temporarily
prevent Python from using the user site directory.

Regards,
Marcel



Matthew Cserhati

unread,
Jun 14, 2021, 2:26:01 PM6/14/21
to pysam-us...@googlegroups.com
Hello Marcel,

I erased all site-package directories, and tried reinstalling pysam, but no luck.
I do not have any ~/.local/lib/python* directories.
I also set "export PYTHONNOUSERSITE=1", and tried running my python script but the error persist.

What else can I try?

Thanks,

Matt

--
You received this message because you are subscribed to a topic in the Google Groups "Pysam User group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pysam-user-group/vS32scr5jGc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pysam-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pysam-user-group/c9569f79-71d7-3dd4-dc0b-29c80f021405%40scilifelab.se.

IMPORTANT WARNING: This email (and any attachments) is only intended for the use of the person or entity to which it is addressed, and may contain information that is privileged and confidential. You, the recipient, are obligated to maintain it in a safe, secure and confidential manner. Unauthorized redisclosure or failure to maintain confidentiality may subject you to federal and state penalties. If you are not the intended recipient, please immediately notify us by return email, and delete this message from your computer.

Marcel Martin

unread,
Jun 14, 2021, 2:41:08 PM6/14/21
to pysam-us...@googlegroups.com
On 14/06/2021 20.25, Matthew Cserhati wrote:
> I erased all site-package directories, and tried reinstalling pysam, but
> no luck.
> I do not have any ~/.local/lib/python* directories.
> I also set "export PYTHONNOUSERSITE=1", and tried running my python
> script but the error persist.

Are you on Linux or macOS? What does the command that I gave in my
previous mail output? Did you write the script yourself? (Do you know
how to program in Python?) Which exact command did you use for installation?

Posting more extensive log messages might also be helpful.

Marcel

Matthew Cserhati

unread,
Jun 14, 2021, 3:02:39 PM6/14/21
to pysam-us...@googlegroups.com
Hi Marcel,

Thanks again for your help.

1. I am on Linux
2. I wrote the python script myself and it has run successfully previously, but now it doesn't.
3. I suspect it is as you say a broken installation
4. I installed pysam-0.16.01 this way:    pip install pysam          at the command prompt
5. error message:

Traceback (most recent call last):
  File "pysam.py", line 40, in <module>
    main(sys.argv[1:])
  File "pysam.py", line 33, in main

    samfile = pysam.AlignmentFile(infile,"rb")
AttributeError: 'module' object has no attribute 'AlignmentFile'

================

the script:

#!/usr/bin/python

import re, os, sys, getopt;
import pysam;

def main(argv):
    infile = ''
    outfile = ''
    chrom, start, end = 0, 0, 0

    try:
        opts, args = getopt.getopt(argv,"hi:o:c:s:e:",["ifile=","ofile=","chrom=","start=","end="])
    except getopt.GetoptError:
        print ('pysam.py -i <inputfile> -o <outputfile> -c <chrom> -s <start> -e <end>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print ('pysam.py -i <inputfile> -o <outputfile> -c <chrom> -s <start> -e <end>')
            sys.exit()
        elif opt in ("-i", "--ifile"):
            infile = arg
        elif opt in ("-o", "--ofile"):
            outfile = arg
        elif opt in ("-c", "--chrom"):
            chrom = int(arg)
        elif opt in ("-s", "--start"):
            start = (arg)
        elif opt in ("-e", "--end"):
            end = arg

    c = 0

    samfile = pysam.AlignmentFile(infile,"rb")
    for read in samfile.fetch(chrom, start, end):
        c += 1

    print(c)

if __name__ == "__main__":
    main(sys.argv[1:])

--
You received this message because you are subscribed to a topic in the Google Groups "Pysam User group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pysam-user-group/vS32scr5jGc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pysam-user-gro...@googlegroups.com.

Marcel Martin

unread,
Jun 14, 2021, 3:07:19 PM6/14/21
to pysam-us...@googlegroups.com
On 14/06/2021 21.02, Matthew Cserhati wrote:
> Traceback (most recent call last):
>   File "pysam.py", line 40, in <module>
>     main(sys.argv[1:])
>   File "pysam.py", line 33, in main
>     samfile = pysam.AlignmentFile(infile,"rb")
> AttributeError: 'module' object has no attribute 'AlignmentFile'
[...]
>         print ('pysam.py -i <inputfile> -o <outputfile> -c <chrom> -s

Is your script actually called 'pysam.py'? Then it will import itself
when you run "import pysam". Try to rename the file.

Marcel

Matthew Cserhati

unread,
Jun 14, 2021, 3:10:19 PM6/14/21
to pysam-us...@googlegroups.com
renamed script but still gives error

--
You received this message because you are subscribed to a topic in the Google Groups "Pysam User group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pysam-user-group/vS32scr5jGc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pysam-user-gro...@googlegroups.com.

Matthew Cserhati

unread,
Jun 14, 2021, 3:13:05 PM6/14/21
to pysam-us...@googlegroups.com
now I get this:

(env) [mcserhati@ftlxsvr08 umi]$ python psam.py -h

Traceback (most recent call last):
  File "psam.py", line 4, in <module>
    import pysam;
ImportError: bad magic number in 'pysam': b'\x03\xf3\r\n'

Marcel Martin

unread,
Jun 14, 2021, 3:55:50 PM6/14/21
to pysam-us...@googlegroups.com
On 14/06/2021 21.12, Matthew Cserhati wrote:
> now I get this:
>
> (env) [mcserhati@ftlxsvr08 umi]$ python psam.py -h
> Traceback (most recent call last):
>   File "psam.py", line 4, in <module>
>     import pysam;
> ImportError: bad magic number in 'pysam': b'\x03\xf3\r\n'

We took this off list, and Matthew now got it working by deleting the
.pyc files.

Marcel

Reply all
Reply to author
Forward
0 new messages