Module not found, but only in ipython3 - python

I have written a package named biographs with the following architecture:
biographs (folder)
>biographs (package)
>__init__.py
>bpdb.py
>pmolecule.py
>bgraph.py
>bspace.py
The __init__.py file is only the following:
from .pmolecule import Pmolecule
When I'm working in ipython3 and I want to import biographs (only to use the class Pmolecule), I get the following error in ipython3 (Ipython 6.0.0, Python 3.6.1):
In [1]: cd ~/biographs/
/Users/rdora/biographs
In [2]: import biographs
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-1803e6928e0e> in <module>()
----> 1 import biographs
/Users/rdora/biographs/biographs/__init__.py in <module>()
----> 1 from .pmolecule import Pmolecule
/Users/rdora/biographs/biographs/pmolecule.py in <module>()
1 # class to deal with protein structures
2 # python 2
----> 3 import bpdb
4 import bgraph
5 import bspace
ModuleNotFoundError: No module named 'bpdb'
However when I do exactly the same process using IPython 5.3.0 with Python 2.7.13 there is no error message.
Thank you

This is because of how imports work in Python 2 and Python 3. In your module pmolecule.py you apparently do import bpdb. In Python 2 this will search the local directory for a module called bpdb.py and import it. However in Python 3 you must be explicit about those relative imports, i.e. you need to do
from . import bpdb
In order to get consistency for Python 2 you can use from __future__ import absolute_imports which prohibits such non-explicit imports also under Python 2.
Note that the same holds for:
----> 3 import bpdb
4 import bgraph
5 import bspace
Those need to be imported via from . import <module-name> syntax.
Further reading
What's new in Python 3 -- Removed Syntax
PEP 328 -- Imports: Multi-Line and Absolute/Relative

Related

No module found error while importing python in notebook

I want to acess m_rcnn.py file in jupyter notebook. The folder structure is as follows
collab_mask_rcnn(working directory). Inside this workinhg directory i have a folder name maskrcnn_colab and inside this maskrcnn_colab folder i have another folder named mrcnn_demo where i have all the python file like m_rcnn.py, config.py located which i am trying to import
import sys
sys.path.append('C:/Users/BIUTeamUser3/Desktop/collab_mask_rcnn/maskrcnn_colab/mrcnn_demo')
from m_rcnn import *
When i excute this i get the following error
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-3841d2b3769e> in <module>
3 import sys
4 sys.path.append("C:/Users/BIUTeamUser3/Desktop/collab_mask_rcnn/maskrcnn_colab/mrcnn_demo")
----> 5 from m_rcnn import *
6 get_ipython().run_line_magic('matplotlib', 'inline')
C:/Users/BIUTeamUser3/Desktop/collab_mask_rcnn/maskrcnn_colab/mrcnn_demo\m_rcnn.py in <module>
19 # Import Mask RCNN
20 sys.path.append(ROOT_DIR) # To find local version of the library
---> 21 from mrcnn_demo.config import Config
22 from mrcnn_demo import utils
23 import mrcnn_demo.model as modellib
ModuleNotFoundError: No module named 'mrcnn_demo'
What am i missing?
I think you use sys.path.insert . Something like this:
from m_rcnn import *
import sys
sys.path.insert(1,'C:/Users/BIUTeamUser3/Desktop/collab_mask_rcnn/maskrcnn_colab/mrcnn_demo')
You might just try it like this:
from maskrcnn_colab.mrcnn_demo.m_rcnn import *

stackstac on Google Colab results in ImportError: cannot import name 'Protocol' from 'typing'

I'm unable to install stackstac on Google Colab. This is reproducible with the code below.
!pip install stackstac
import stackstac
outputs:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-c01f370957f7> in <module>()
1 get_ipython().system('pip install stackstac')
----> 2 import stackstac
1 frames
/usr/local/lib/python3.7/dist-packages/stackstac/rio_reader.py in <module>()
5 import threading
6 import weakref
----> 7 from typing import TYPE_CHECKING, Optional, Protocol, Tuple, Type, Union
8
9 import numpy as np
ImportError: cannot import name 'Protocol' from 'typing' (/usr/lib/python3.7/typing.py)
---------------------------------------------------------------------------
Same issue on a local instance how ever that solution doesn't translate.
Protocol was introduced to typing as of Python 3.8, as can be seen in the docs. You appear to be running Python 3.7, based on your file paths - upgrade to use Python 3.8 or later if you can.

ModuleNotFoundError: No module named '_future_'

What should I do solving this print_function eror in anaconda.
I write the below code,
from _future_ import print_function
But it show an error and that is,
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-fd1a0773c2f5> in <module>
----> 1 from _future_ import print_function
ModuleNotFoundError: No module named '_future_'
Note: I uam using created environment as py36. because I it was to difficult to install ensorflow and keras where I used Python 3.7 . That's why I create new environment where python version is Python 3.6.0 .
There are two underscores on both sides of Python's __future__ module, so your import should be:
from __future__ import print_function

dtreeviz: from graphviz.backend cannot import name 'run'

dtreeviz library, ImportError: cannot import name 'run'
I tried to follow this instruction on Github to install and import the dtreeviz library.
When I run the where dot and dot -V command both seem to work fine as you can see here Lines in Windows Terminal.
However when I try to import dtreeviz in Python (Anaconda, version 3.6.5) I get the following error:
from dtreeviz.trees import *
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-fd764fe550c7> in <module>()
1 from sklearn import tree
----> 2 from dtreeviz.trees import *
C:\Users\marcb\Anaconda3\lib\site-packages\dtreeviz\trees.py in <module>()
6 from pathlib import Path
7 from sklearn import tree
----> 8 from graphviz.backend import run, view
9 import matplotlib.pyplot as plt
10 from dtreeviz.shadow import *
ImportError: cannot import name 'run'
I hope that someone can help me as I want to use this library to visualize single trees in Python and this library offers much more features than the original graphviz package.
Note: This is my first post on Stackoverflow, so feedback on this is also welcome.
I don't know if you are still looking for an awser, but i had exactly the same problem and fixed it this way:
In ~/.local/lib/python3.8/site-packages/dtreeviz/trees.py' i replaced the line
79: execute(' '.join(cmd), capture_output=True, check=True, quiet=False)
by
79: os.system(' '.join(cmd)).

Not able to import Localization library in python 3.7

I am trying to import Localization library in python 3.7 in Jupyter notebooks but everytime it shows this error:
ModuleNotFoundError
Traceback (most recent call last)
<ipython-input-6-ca0ae5a2ecfb> in <module>()
2 from skimage.measure import regionprops
3 import matplotlib.patches as patches
----> 4 import localization
~\Anaconda3\lib\site-packages\localization\__init__.py in <module>()
----> 1 from geoProject import *
ModuleNotFoundError: No module named 'geoProject'
I have no project named 'geoProject' either.
It is not your fault. geoProject seems to be another file in that project.
https://github.com/kamalshadi/Localization/blob/master/localization/geoProject.py
It is not being imported correctly inside the localization module at the __init__.py file

Categories

Resources