ImportError Cannot import name - nonspecific - python

I have made python package from
https://github.com/raamana/pyradigm
and uploaded it to pip using the following commands (after following the instructions on pypi, etc):
python setup.py sdist bdist_wheel
twine upload dist/*
In theory, now one should be able to do
pip install pyradigm
(which I can confirm works) and then do the following in a Python file:
from pyradigm import MLDataset
to use the class MLDataset from pyradigm
Unfortunately it's not the case, as Python is throwing the following ImportError which is fairly non-specific:
In [1]: from pyradigm import MLDataset
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-4ad7330da13e> in <module>()
----> 1 from pyradigm import MLDataset
ImportError: cannot import name MLDataset
I can confirm that
I was able to install pyradigm via pip on a new system
both pip and python are able to find the package (installed in site-packages), as they auto-fill both the module name (pyradigm and class name MLDataset).
The setup.py in pyradigm is this:
#!/usr/bin/env python
import os
from setuptools import setup, find_packages
# Utility function to read the README file.
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(name='pyradigm',
version='0.1.1.2',
description='Python-based data structure to improve handling of datasets in machine learning workflows',
long_description=read('README.md'),
keywords='machine learning, test dataset, python, workflow, provenance, data structure',
author='Pradeep Reddy Raamana',
url='https://github.com/raamana/pyradigm',
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]), # packages=['pyradigm'],
install_requires=['numpy', 'setuptools'],
)
The package has only 3 files:
__init__.py pyradigm.py test_pyradigm.py
with the __init__.py containing the only a single line:
__all__ = [ 'pyradigm', 'MLDataset' ]
Any help in understanding in this error, and ways to fix it would be appreciated. Thanks.

It's because you have a pyradigm module inside a pyradigm package.
I've installed the package in a virtualenv using pip install pyradigm and successfully imported MLDataset with
from pyradigm.pyradigm import MLDataset
If you want to import as
from pyradigm import MLDataset
update your __init__.py to
from pyradigm import MLDataset

Related

CHEMBL : Can't import new_client from chembl_webresource_client.new_client

I have installed the chembl_webresource_client package.
Then I tried to import a module from the package:
from chembl_webresource_client.new_client import new_client
But it fails to execute and this error appears:
ImportError Traceback (most recent call
last) in ()
1 # Import necessary libraries
2 import pandas as pd
----> 3 from chembl_webresource_client.new_client import new_client
4 frames
/usr/local/lib/python3.7/dist-packages/chembl_webresource_client/cache.py
in ()
1 author = 'mnowotka'
2
----> 3 from requests_cache.backends.base import BaseCache, hashlib, _to_bytes
4
5 def create_key(self, request):
ImportError: cannot import name 'hashlib' from
'requests_cache.backends.base'
(/usr/local/lib/python3.7/dist-packages/requests_cache/backends/base.py)
Is there a fix for this?
I have faced the same issue today and resolved it by updating chembl-webresource-client library's version from 0.10.2 to 0.10.3 in my project's requirements.txt file.
For example: chembl-webresource-client==0.10.3
Also after making these changes and activating your project's virtual environment, please don't forget to fetch and re-install all listed libraries in your requirements.txt using the following command:
pip install -r .\requirements.txt

Import own Python module on Kaggle

I am new to this and very new to Kaggle.
I am trying to replicate this notebook:
https://www.kaggle.com/bibhash123/chest-x-ray-abnormalities-baseline-tf-keras
There one person wrote a module from utilities_x_ray
https://www.kaggle.com/bibhash123/utilities-x-ray
How to import it? It looks like utilities_x_ray is not directly specified in the second link.
As a result, I get an error
ERROR: Could not find a version that satisfies the requirement utilities_x_ray
ERROR: No matching distribution found for utilities_x_ray
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-14-79d7808e32c5> in <module>
11 import matplotlib.pyplot as plt
12 get_ipython().system('pip install utilities_x_ray')
---> 13 import utilities_x_ray
14 from utilities_x_ray import read_xray,showXray
15 from tqdm import tqdm
ModuleNotFoundError: No module named 'utilities_x_ray'
You do not need to pip install the utility file. Rather, just include the utilities_x_ray.py file in the same directory that you run the python notebook.
The python notebook looks for files within the same directory whenever importing as well as those modules that were installed via pip.
You need to copy the script content from https://www.kaggle.com/bibhash123/utilities-x-ray into your working directory and save it as utilities_x_ray.py
Hope this w'll be helpfull
You should consider downloading both the chest-x-ray-abnormalities-baseline-tf-keras.ipynb as and the utilities_x_ray.py Please make sure that they are into the same folder... After that, you'll need to install the pydimcom and scikit-image using pip install pydicom scikit-image
You can also install the packages directly from the GIt repository:
!pip install git+https://github.com/dkriegner/xrayutilities.git

Package listed with pip but cannot be imported

I am writing my own package for a test with setup.py. I tried to install it on my own computer with python3 setup.py install, and there was no error. I checked with pip3 list and found it there,
steplib 0.1.0
However, when I tried importing it in a python interpreter, I could not,
>>> import steplib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'steplib'
Using help('modules') confirmed this as it was not shown in the list.
Am I doing anything wrong? Do I have to register it first before using it?
edit
My site packages contains steplib-0.1.0-py3.7.egg-info. It does not have any sub-packages (it is a test package):
steplib
__init__.py
steplib.py
setup.py
Ideally module should go into your site packages. Do you see it there?
If its there then check your package path. You package may be steplib, but have you checked if you are importing right package and module there. For example within your steplib folder you might have additional package and module within it. Say package is X and module is Y. Then you can import it as below.
from X import Y
Make sure you have init.py in your package to qualify that as a package.
Did you install the package in the python directory under Lib?

Python module from GitHub installed using setup.py can't see own submodules

I downloaded python-somelib-master.zip from GitHub hoping to use the API it provides. I ran
python setup.py install
And it apparently completed successfully:
Writing D:\SOFT\Python3\Lib\site-packages\python-somelib-1.0-py3.5.egg-info
which then introduced D:\SOFT\Python3\Lib\site-packages\somelib.
However, when I try to import something from there in my script:
from somelib import SubModule
I get
File "D:\SOFT\Python3\lib\site-packages\somelib\__init__.py", line 1, in <module>
from base import SubModule
ImportError: No module named 'base'
I confirmed that base.py is present in D:\SOFT\Python3\Lib\site-packages\somelib.
Did I not properly install the module?
You must install modules dependencies in your machine too. If you are using Ubuntu, you can easily do it with "apt-get". Hope it helps! xD

'from gensim import test' is not importing successfully

I installed gensim, Python library.
I executed the command
Import gensim
It executed without any error.
Then I tried to import test from gensim using the command
from gensim import test
and it showed the following error
Traceback (most recent call last):
File "", line 1, in
from gensim import test
ImportError: cannot import name 'test'
Python site-packages had gensim folder in that.
Any help would be highly appreciated.
As it says: cannot import name 'test'
That means that test is not in gensim package.
You can see package's modules with:
import gensim
print(gensim.__all__) # when package provide a __all__
or
import gensim
import pkgutil
modules = pkgutil.iter_modules(gensim.__path__)
for module in modules:
print(module[1])
Edit:
How can I verify gensim installation ?
try:
import gensim
except NameError:
print('gensim is not installed')
Side note: if you have a file or package named gensim, this will ne imported instead of the real package.
I had similar experience but with scipy. After uninstalling scipy import scipy did not give error. but none of its modules will work. I noticed that scipy folder was still in \site-packages but modules inside it were uninstalled.
When I installed it again it worked fine.
You should also see inside the directory, and try to reinstall or upgrade it.

Categories

Resources