Question: python script error: ImportError: No module named pysam
1
gravatar for jaqx008
5 months ago by
jaqx00840
jaqx00840 wrote:

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,
ADD COMMENTlink modified 5 months ago by Carlos Borroto1.8k • written 5 months ago by jaqx00840

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 REPLYlink written 5 months ago by WouterDeCoster32k

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 REPLYlink written 5 months ago by Carlos Borroto1.8k

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

ADD REPLYlink written 5 months ago by jaqx00840
1
gravatar for WouterDeCoster
5 months ago by
Belgium
WouterDeCoster32k wrote:

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 COMMENTlink written 5 months ago by WouterDeCoster32k
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 REPLYlink written 5 months ago by jaqx00840

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

ADD REPLYlink written 5 months ago by WouterDeCoster32k

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 REPLYlink written 5 months ago by jaqx00840

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

ADD REPLYlink written 5 months ago by WouterDeCoster32k

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

ADD REPLYlink written 5 months ago by jaqx00840
1

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 REPLYlink written 5 months ago by Vimalkumar Velayudhan190

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 REPLYlink written 5 months ago by jaqx00840

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 REPLYlink modified 4 months ago • written 4 months ago by WouterDeCoster32k

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 REPLYlink written 4 months ago by Devon Ryan85k

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 REPLYlink written 5 months ago by WouterDeCoster32k

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 REPLYlink modified 5 months ago • written 5 months ago by jaqx00840

Have you read and tried what I've written?

ADD REPLYlink written 5 months ago by WouterDeCoster32k

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

ADD REPLYlink written 5 months ago by jaqx00840

doesn't work

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

ADD REPLYlink written 5 months ago by WouterDeCoster32k
1

Ok

ADD REPLYlink written 5 months ago by jaqx00840

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

which python2.7
ADD REPLYlink written 5 months ago by WouterDeCoster32k

/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 REPLYlink modified 5 months ago • written 5 months ago by jaqx00840

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 REPLYlink modified 5 months ago • written 5 months ago by WouterDeCoster32k

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 REPLYlink modified 5 months ago • written 5 months ago by jaqx00840

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 REPLYlink modified 5 months ago • written 5 months ago by WouterDeCoster32k

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 REPLYlink written 5 months ago by jaqx00840

Could you use a github gist to share the error?

ADD REPLYlink written 5 months ago by WouterDeCoster32k

I just posted it on GitHub. Thanks

ADD REPLYlink written 5 months ago by jaqx00840

Great. Can you share the link?

ADD REPLYlink written 5 months ago by WouterDeCoster32k

Below is the link. sorry for the troubles.

ADD REPLYlink modified 5 months ago by genomax57k • written 5 months ago by jaqx00840

And what about

python2.7 -m pip install pysam
ADD REPLYlink written 5 months ago by WouterDeCoster32k

ADD REPLYlink written 5 months ago by jaqx00840

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 REPLYlink written 5 months ago by jaqx00840

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 REPLYlink written 4 months ago by Devon Ryan85k
1

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 REPLYlink written 4 months ago by cpad01129.3k
which -a gcc

/usr/bin/gcc

ADD REPLYlink written 4 months ago by jaqx00840

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 REPLYlink modified 5 months ago • written 5 months ago by jaqx00840
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1110 users visited in the last hour