AttributeError: module 'tensorly' has no attribute 'decomposition' - python

I’m using a package (tensorly) on python where I don't have access to all the modules.
For example if I try to use the 'decomposition' module :
python version: 3.9.12
tensorly version : 0.7
I run :
pip3 install tensorly
python3 main.py
main.py :
### imports ###
import tensorly
### tensor decomposition ###
cp = tensorly.decomposition.CP(n)
output :
AttributeError: module 'tensorly' has no attribute 'decomposition'
PS: When I go to /.local/lib/python3.9/site-packages/tensorly there is the module decomposition and when I print my sys.path there is the path for this same site-packages.
I have the same problem with another package (cobrapy) and on other different machines with other versions of python (3.6)
Update :
Now I have the exact same problem with scikit-learn:
from sklearn.preprocessingcessing import StandardScaler
Output :
No module named 'sklearn.preprocessingcessing'
Even if this package worked really well before (no error with .preprocessingcessing), this error popped out randomly today...

You have to first import the submodule you want to use if it isn't loaded by default (you can check the __init__.py file to see what modules are imported by default).
In other words, just first import decomposition:
import tensorly
import tensorly.decomposition
Or directly import the decomposition methods you want to use:
from tensorly.decomposition import CP
You also have a typo in your Scikit-Learn example.

Related

can't import kornia.augmentation.functional

I have installed kornia and imorting it like,
from kornia.color import *
import kornia.augmentation.functional as F_k
import kornia as K
but the second line is giving error
ModuleNotFoundError: No module named 'kornia.augmentation.functional'.
Also, this is my directory structure.
But I getting error
ModuleNotFoundError: No module named 'FewShot_models'
when I try to import from FewShot_models.manipulate import *.
I am following a code from github and trying to implement that.
kornia.augmentation.functional was removed in version 0.5.4 and the most of the functions are available through kornia.augmentation.
Regarding your second question, you need to add empty file named __init__.py to FewShot_models directory. Check this answer for details about __init__.py.

How do I find details of this python package (__main__)? [duplicate]

Currently trying to work in Python3 and use absolute imports to import one module into another but I get the error ModuleNotFoundError: No module named '__main__.moduleB'; '__main__' is not a package. Consider this project structure:
proj
__init__.py3 (empty)
moduleA.py3
moduleB.py3
moduleA.py3
from .moduleB import ModuleB
ModuleB.hello()
moduleB.py3
class ModuleB:
def hello():
print("hello world")
Then running python3 moduleA.py3 gives the error. What needs to be changed here?
.moduleB is a relative import. Relative only works when the parent module is imported or loaded first. That means you need to have proj imported somewhere in your current runtime environment. When you are are using command python3 moduleA.py3, it is getting no chance to import parent module. You can:
from proj.moduleB import moduleB OR
You can create another script, let's say run.py, to invoke from proj import moduleA
Good luck with your journey to the awesome land of Python.
Foreword
I'm developing a project which in fact is a Python package that can be installed through pip, but it also exposes a command line interface. I don't have problems running my project after installing it with pip install ., but hey, who does this every time after changing something in one of the project files? I needed to run the whole thing through simple python mypackage/main.py.
/my-project
- README.md
- setup.py
/mypackage
- __init__.py
- main.py
- common.py
The different faces of the same problem
I tried importing a few functions in main.py from my common.py module. I tried different configurations that gave different errors, and I want to share with you with my observations and leave a quick note for future me as well.
Relative import
The first what I tried was a relative import:
from .common import my_func
I ran my application with simple: python mypackage/main.py. Unfortunately this gave the following error:
ModuleNotFoundError: No module named '__main__.common'; '__main__' is not a package
The cause of this problem is that the main.py was executed directly by python command, thus becoming the main module named __main__. If we connect this information with the relative import we used, we get what we have in the error message: __main__.common. This is explained in the Python documentation:
Note that relative imports are based on the name of the current module. Since the name of the main module is always __main__, modules intended for use as the main module of a Python application must always use absolute imports.
When I installed my package with pip install . and then ran it, it worked perfectly fine. I was also able to import mypackage.main module in a Python console. So it looks like there's a problem only with running it directly.
Absolute import
Let's follow the advise from the documentation and change the import statement to something different:
from common import my_func
If we now try to run this as before: python mypackage/main.py, then it works as expected! But, there's a caveat when you, like me, develop something that need to work as a standalone command line tool after installing it with pip. I installed my package with pip install . and then tried to run it...
ModuleNotFoundError: No module named 'common'
What's worse, when I opened a Python console, and tried to import the main module manually (import mypackage.main), then I got the same error as above. The reason for that is simple: common is no longer a relative import, so Python tries to find it in installed packages. We don't have such package, that's why it fails.
The solution with an absolute import works well only when you create a typical Python app that is executed with a python command.
Import with a package name
There is also a third possibility to import the common module:
from mypackage.common import my_func
This is not very different from the relative import approach, as long as we do it from the context of mypackage. And again, trying to run this with python mypackage/main.py ends similar:
ModuleNotFoundError: No module named 'mypackage'
How irritating that could be, the interpreter is right, you don't have such package installed.
The solution
For simple Python apps
Just use absolute imports (without the dot), and everything will be fine.
For installable Python apps in development
Use relative imports, or imports with a package name on the beginning, because you need them like this when your app is installed. When it comes to running such module in development, Python can be executed with the -m option:
-m mod : run library module as a script (terminates option list)
So instead of python mypackage/main.py, do it like this: python -m mypackage.main.
In addition to md-sabuj-sarker's answer, there is a really good example in the Python modules documentation.
This is what the docs say about intra-package-references:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.
If you run python3 moduleA.py3, moduleA is used as the main module, so using the absolute import looks like the right thing to do.
However, beware that this absolute import (from package.module import something) fails if, for some reason, the package contains a module file with the same name as the package (at least, on my Python 3.7). So, for example, it would fail if you have (using the OP's example):
proj/
__init__.py (empty)
proj.py (same name as package)
moduleA.py
moduleB.py
in which case you would get:
ModuleNotFoundError: No module named 'proj.moduleB'; 'proj' is not a package
Alternatively, you could remove the . in from .moduleB import, as suggested here and here, which seems to work, although my PyCharm (2018.2.4) marks this as an "Unresolved reference" and fails to autocomplete.
Maybe you can do this before importing the module:
moduleA.py3
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from moduleB import ModuleB
ModuleB.hello()
Add the current directory to the environment directory
Just rename the file from where you run the app to main.py:
from app import app
if __name__ == '__main__':
app.run()
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
will solve the issue of import path issue.

Cannot import name 'GeoText' from the geotext Python package

I have installed the geotext module from pip.
However this line:
from geotext import GeoText
produces this error:
ImportError: cannot import name 'GeoText'
The module structure is as follows:
where there is no sub directory but directly the geotext python file containing the GeoText class:
The correct syntax of "from module import Class" is used in the __init__.py file too.
from geotext import GeoText
Could anyone help me interpret as to why the error could be generated in such a scenario where there seems to be no circular imports?
The import statement from the init file needs to be removed. This corrects the circular import scenario.
I had the same problem.
Installing geotext like so fixed it for me:
python3 -m pip install geotext

Tensorflow translate.py import error: No module named translate

I'm trying to run Tensorflow's translate.py from a python console rather than through bazel -build, but I get an error at these two lines:
from tensorflow.models.rnn.translate import data_utils
from tensorflow.models.rnn.translate import seq2seq_model
ImportError: No module named translate
I've checked the folder to see that the "init.py" file is there, but python seems to think there is no such module as translate.
How can i fix this?
The best way to do this is to navigate to folder containing the translate module and running it. You can also download the translate module to any other place and run it. However, don't forget to change the above lines to:
from translate import data_utils
from translate import seq2seq_model
I resolved this issue by removing all the from tensorflow.models.rnn.translate statements, leaving just
import data_utils
import seq2seq_model
in translate.py and
import data_utils
in seq2seq_model.py.

Python ImportError: cannot import name datafunc [PyML]

I have installed PyML package in order to use some machine learning algorithms, and according to the tutorial, my installation is successful.
I try to run a python script which includes the following line to import modules from PyML
from PyML import datafunc,svm,assess,modelSelection,ker
However I get the error message above saying
File <stdin>, line 1, in <module> ImportError: cannot import name
datafunc
cannot import name datafunc`. From terminal I check every module by saying
from PyML import datafunc,
from PyML import svm,
from PyML import ker
I only get error message for datafunc. The PyML library is under the site-packages folder of Python 2.7.
I check this question here Python error: ImportError: cannot import name Akismet, but I could't see how it will help my problem.
Do you have any idea why Python imports some modules but does not import this one?
In PyML-0.7.13.3, the datafunc module exists in PyML/containers directory.
So it seems that you can import the module as follows:
from PyML.containers import datafunc
Howerver, it raises an error beacuse the datafunc module uses
undefined classes BaseVectorDataSet and SparseDataSet.
Thus you need to modify the source of PyML
in order to use datafunc module.
First, prepend the following two lines to PyML/containers/datafunc.py
and re-install the PyML library.
from PyML.containers.baseDatasets import BaseVectorDataSet
from PyML.containers.vectorDatasets import SparseDataSet
Then you can import the modules as follows:
from PyML import svm, modelSelection, ker
from from PyML.containers import datafunc
from from PyML.evaluators import assess
BTW, I recommend that you use more documented and tested machine learning library, such as scikit-learn.

Categories