python script error: ImportError: No module named pysam
1
2
Entering edit mode
6.5 years ago
jaqx008 ▴ 110

Hello all. I have been having issues on running a python script. I have tried to resolve it by uninstalling and reinstalling pysam since the error is about pysam mostly. below is the command and error i get.

$ python2.7 overlapping_reads.py 
Traceback (most recent call last):
  File "overlapping_reads.py", line 4, in <module>
    import pysam
ImportError: No module named pysam

Can someone help?also, belo is portion of my script if that helps. Thanks

import argparse
from collections import defaultdict

import pysam


def Parser():
    the_parser = argparse.ArgumentParser()
    the_parser.add_argument(
        '--input', action="store", type=str, help="bam alignment file")
    the_parser.add_argument(
        '--minquery', type=int,
        help="Minimum readsize of query reads (nt) - must be an integer")
    the_parser.add_argument(
        '--maxquery', type=int,
        help="Maximum readsize of query reads (nt) - must be an integer")
    the_parser.add_argument(
        '--mintarget', type=int,
pysam pythonscript .py overlapsignature • 36k views
ADD COMMENT
0
Entering edit mode

I added code markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:

101010 Button

ADD REPLY
0
Entering edit mode

This is most likely the case where you are using a pip command associated with a different python installation.

Please share the exact command used to install pysam. That could help decide what to do next to figure out where pysam might be getting installed.

ADD REPLY
0
Entering edit mode

this is what I used. conda install -c bioconda pysam=0.9.1

ADD REPLY
2
Entering edit mode
6.5 years ago

I have tried to resolve it by uninstalling and reinstalling pysam

Make sure you install pysam for the right python version. In your case, I suggest:

python2.7 -m pip install pysam
ADD COMMENT
0
Entering edit mode
it gave the same error.

$ python2.7 -m pip install pysam
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip
ADD REPLY
0
Entering edit mode

That's not the same error. You need to install pip.

ADD REPLY
0
Entering edit mode

I uninstall and installed pip again.

sudo easy_install pip
Password:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl#sha256=717cdffb2833be8409433a93746744b59505f42146e8d37de6c62b430e25d6d7
Best match: pip 10.0.1
Processing pip-10.0.1-py2.py3-none-any.whl
Installing pip-10.0.1-py2.py3-none-any.whl to /anaconda3/lib/python3.6/site-packages
writing requirements to /anaconda3/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg/EGG-INFO/requires.txt
Adding pip 10.0.1 to easy-install.pth file
Installing pip script to /anaconda3/bin
Installing pip3 script to /anaconda3/bin
Installing pip3.6 script to /anaconda3/bin

Installed /anaconda3/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg
Processing dependencies for pip
Finished processing dependencies for pip

python2.7 -m pip install pysam
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip
ADD REPLY
1
Entering edit mode

Yes you installed pip for /anaconda3/lib/Python3.6, but not for python2.7

ADD REPLY
0
Entering edit mode

I have tried all I can to install pip for python2.7. all efforts have proved abortive.

ADD REPLY
2
Entering edit mode

You can create a new conda environment with Python 2 and install pysam:

conda create -n pysam -c bioconda python=2 pysam=0.9.1

Then, activate that environment and run your script:

conda activate pysam
python2.7 overlapping_reads.py
ADD REPLY
0
Entering edit mode

I did that. and it gave this.

python2.7 overlapping_reads.py 
Traceback (most recent call last):
  File "overlapping_reads.py", line 4, in <module>
    import pysam
ImportError: No module named pysam
ADD REPLY
1
Entering edit mode

Creating a conda environment should solve your issues actually. Can you try this again and make sure you execute all commands?

conda create -n pysam -c bioconda python=2 pysam
conda activate pysam
python overlapping_reads.py
ADD REPLY
0
Entering edit mode

In addition to Wouter's suggestion of a conda environment, you may need to temporarily rename ~/.local so that packages installed via pip or pip3 aren't loaded by accident. You'll occasionally run into issues otherwise.

ADD REPLY
0
Entering edit mode

If you execute

which easy_install

you will see that your operating system first selects the easy_install from anaconda

You need to make sure that you use the easy_install from python2.7.

For that, you can do

which python2.7

and figure out the directory where the python2.7 executable is. In my case that is /usr/local/bin/python2.7. And the correct easy install would then be /usr/local/bin/easy_install

Do you really need python2.7 here?

ADD REPLY
0
Entering edit mode

for some reason, this python script works with python2.7. I don't know why. but all I want to do is execute the .py script. i do python xxx.py and it doest run because of the pysam in the script.

python2.7 overlapping_reads.py 
Traceback (most recent call last):
  File "overlapping_reads.py", line 4, in <module>
    import pysam
ImportError: No module named pysam

and i if i do

python2.7 -m pip install pysam

I get

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip
ADD REPLY
0
Entering edit mode

Have you read and tried what I've written?

ADD REPLY
0
Entering edit mode

Yes.. I did tried it. but when I get to the point to execute the py script, doesn't work.

ADD REPLY
1
Entering edit mode

doesn't work

Just a minute, I'm getting my crystal ball to see the error message.

ADD REPLY
1
Entering edit mode

Ok

ADD REPLY
0
Entering edit mode

Okay, let's try again. What's the output of

which python2.7
ADD REPLY
0
Entering edit mode

/usr/bin/python2.7 /usr/bin/easy_install

$ /usr/bin/easy_install
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-71297.pth'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /Library/Python/2.7/site-packages/

Perhaps your account does not have write access to this directory?  If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.  If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

For information on other options, you may wish to consult the
documentation at:

  https://pythonhosted.org/setuptools/easy_install.html

Please make the appropriate changes for your system and try again.
ADD REPLY
0
Entering edit mode

The error message makes this quite clear what is going on: you don't have permission to write in that directory and you'll need sudo for this:

sudo /usr/bin/easy_install pip

Please remember, when asking questions and getting help to be as detailed as possible. We cannot guess what is going on if you don't share all information.

ADD REPLY
0
Entering edit mode

Ok Thanks. the last command was able to install pip. But pip wasn't able to install pysam. see bellow.

$ python2.7 -m pip install pysam
Collecting pysam
  Using cached https://files.pythonhosted.org/packages/fc/9b/4bb8d016406dcff47e2866e14d3dcb10741ec3920649e8c521996830944f/pysam-0.14.1.tar.gz
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
Installing collected packages: pysam
  Running setup.py install for pysam ... error
    Complete output from command /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-install-W6N8pp/pysam/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-record-EGbw40/install-record.txt --single-version-externally-managed --compile:
    # pysam: no cython available - using pre-compiled C
    # pysam: htslib mode is shared
    # pysam: HTSLIB_CONFIGURE_OPTIONS=None
    checking for gcc... gcc
    checking whether the C compiler works... yes

code continued..... then ended with

Command "/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-install-W6N8pp/pysam/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-record-EGbw40/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-install-W6N8pp/pysam/
ADD REPLY
0
Entering edit mode

Is it possible to add the full output?

Please remember, when asking questions and getting help to be as detailed as possible. We cannot guess what is going on if you don't share all information.

ADD REPLY
0
Entering edit mode

My bad. I pasted part of it because the forum has a limited character allowed. I tried to post everything but the characters were 5X more than the allowed. again I apologize.

ADD REPLY
0
Entering edit mode

Could you use a github gist to share the error?

ADD REPLY
0
Entering edit mode

I just posted it on GitHub. Thanks

ADD REPLY
0
Entering edit mode

Great. Can you share the link?

ADD REPLY
0
Entering edit mode

Below is the link. sorry for the troubles.

ADD REPLY
0
Entering edit mode

And what about

python2.7 -m pip install pysam
ADD REPLY
0
Entering edit mode

ADD REPLY
0
Entering edit mode

I realize the complain about tornado and nose. so I install this two and hoping python2.7 -m pip install pysam would work. But it didn't still.

ADD REPLY
0
Entering edit mode

What Linux distribution is this? Since you mentioned conda earlier, I suspect that this is using gcc from conda rather than your system, which results in the library search path being broken. Can you post the results of which -a gcc?

ADD REPLY
1
Entering edit mode

I think OP is on OS X.

last line from error (copy/pasted):

 Command "/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-install-qZhLf2/pysam/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-record-uC4wxT/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/tz/89qgqyd97wj1cdrpq_d62n100000gr/T/pip-install-qZhLf2/pysam/
ADD REPLY
0
Entering edit mode
which -a gcc

/usr/bin/gcc

ADD REPLY
0
Entering edit mode

Does this error make sense to you please? when I call pip, it displays pip menu, but when I do pip install pysam I get this

svtyper 0.5.0 has requirement pysam>=0.12.0, but you'll have pysam 0.9.1 which is incompatible.
distributed 1.19.1 has requirement tornado>=4.5.1, but you'll have tornado 4.3 which is incompatible.
ADD REPLY

Login before adding your answer.

Traffic: 1698 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6