"import ... as .." trigger AttributeError - python

when I use "import as" like
import tensorflow.python.ops.control_flow_ops as ass
I get "AttributeError: module 'tensorflow' has no attribute 'python'"
but code below like
import tensorflow.python.ops.control_flow_ops
works fine
env: python 3.6.3 + tensorflow 1.3.0

This is an unfortunate combination of TensorFlow's interface sealing (e.g. removing tf.python symbol visibility from import tensorflow as tf) and Python import semantics. The import tensorflow... as syntax will only access public TensorFlow symbols, while from tensorflow... import ... as can also access private symbols with no API stability guarantees.

Related

AttributeERROR : module'tensorflow.keras.applicationsas no attribute efficientnet_v2

When i try to run the efficientNetv2 model
I got this erreur Error-message
AttributeError: module'tensorflow.keras.applications ' has no attribute 'efficientnet_v2'
Tensorflow version : tensorflow-gpu:2.6
The import is incorrect, you need to update it, it might have worked in older Keras versions,but the internal per-network modules inside keras.applications are not exposed anymore, so your correct import would be:
keras.applications.EfficientNetV2S
Or if you use tf.keras:
tf.keras.applications.EfficientNetV2S
For future reference, always check the documentation, for EfficientNetV2S the link is here.
install efficientnet in you env
!pip install keras-efficientnet
then you can import model as
import efficientnet.tfkeras as efc
done...
you can use prefix 'efc' for B0-B7

Import from Github : How to fix ImportError

I want to use the open source person re-identification library in Python
on Ubuntu 19.04
with Anaconda
no CUDA
in the terminal PyCharm (or not)
Python version 3.7.3
PyTorch version 1.1.0
For that I have to follow instruction like on their deposite git :
git clone https://github.com/Cysu/open-reid.git
cd open-reid
python setup.py install
python examples/softmax_loss.py -d viper -b 64 -j 2 -a resnet50 --logs-dir logs/softmax-loss/viper-resnet50
I receive the following error:
from sklearn.utils.extmath
import pinvh
ImportError: cannot import name 'pinvh'
I have tried to create virtual environments with previous versions of PyTorch (0.4.1, 0.4.0 and 1.0.1) but I always got:
File "examples/softmax_loss.py", line 12, in <module>
from reid import datasets
ModuleNotFoundError: No module named 'reid'
I do not know how to fix it.
EDIT :
Hi thanks for the answer, the problem is that the import are like :
from reid import datasets
from reid import models
from reid.dist_metric import DistanceMetric
from reid.trainers import Trainer
from reid.evaluators import Evaluator
from reid.utils.data import transforms as T
from reid.utils.data.preprocessor import Preprocessor
from reid.utils.logging import Logger
from reid.utils.serialization import load_checkpoint, save_checkpoint
I tried :
from ../reid import datasets
But I got a
File "examples/softmax_loss.py", line 12
from ../reid import datasets
^
SyntaxError: invalid syntax
EDIT 2 :
After re-installing Python 3.7.3 and pytorch 1.1.0 the problem persist with pinvh... I still got this message :
ImportError: cannot import name 'pinvh' from 'sklearn.utils.extmath'
If you can tell me how to fix it or try to tell me if it works please
Since the directory structure is as below:
/(root)-->|
|
|-->reid |--> (contents inside reid)
|
|
|-->examples | -->softmax_loss.py
|
|-->(Other contents in root directory)
It can be observed that reid is not in the same directory as softmax_loss.py, but instead in the parent directory.
So, in the file softmax_loss.py, at line number 12 and below, replace reid with ../reid, this looks for the directory reid in the parent directory.
The other method is to use: import ../reid as R or any other variable; Then use from R import datasets, and so on
utils.extmath.pinvh was deprecated in scikit-learn version 0.19 and removed in version 0.21. The easy fix is therefore to use an earlier version of scikit-learn.

"from a import b" not working, but I can import a and then use a.b

Overview
from elasticsearch import Elasticsearch does not work.
import elasticsearch
e = elasticsearch.Elasticsearch(...)
does work.
Deets
I am trying to use a simple Elasticsearch client in python using AWS (ssh'd on an Amazon linux e3 machine). The code I am copying is here. I am unable to import the Elasticsearch class as described in the guide.
Using from elasticsearch import Elasticsearch gives me the error: ImportError: cannot import name 'Elasticsearch'.
I opened the python3 cli to check it out. If I type from elasticsearch import E and tab-complete, I get the following suggestions: EOFError( Ellipsis EnvironmentError( Exception(. However from elasticsearch import Ellipsis gives me ImportError: cannot import name 'Ellipsis'.
If I type import elasticsearch, then on the next line elasticsearch. and hit tab to autocomplete, I get the full range that I would expect (Elasticsearch(, RequestsHttpConnection(, etc.).
I assume that this has something to do with the way/where it is installed.
I used pip3 install elasticsearch --user to install it originally. I uninstalled it (pip3 uninstall elasticsearch) and returned to the python cli. from elasticsearch import E still gives me EOFError( Ellipsis EnvironmentError( Exception( on the tab-complete, but from elasticsearch import Ellipsis now returns ModuleNotFoundError: No module named 'elasticsearch', as does just import elasticsearch.
Not really quite sure what is up. I did not tag this as elasticsearch because it might be a user error :P
which python3: /usr/bin/python3
which pip3: ~/.local/bin/pip3
pip3 --version: pip 18.1 from /home/ec2-user/.local/lib/python3.6/site-packages/pip (python 3.6)
My problem was that I had named my file the same thing as the module I was trying to import from - elasticsearch.py. As user2357112 states, I became hung up on the incorrect autocomplete.

pattern.en Unresolved import

I used pip install pattern to pattern in my virtualenv (python 2.7). It was installed with following message: Successfully uninstalled Pattern-2.6. When I try to import it in my PyDev project in Eclipse as import pattern, it works fine. But if I try to import en package as import pattern.en, it shows me unresolved import
This happens because the static analyzer doesn't know that the pattern lib actually changes the path at runtime so that pattern.text.en becomes pattern.en.
The related code is:
try:
__path__.append(os.path.join(__path__[0], "text"))
except:
pass
The workaround is actually doing import pattern.text.en or adding pattern.en to the forced builtins as explained in: http://www.pydev.org/manual_101_interpreter.html#PyDevInterpreterConfiguration-ForcedBuiltins
Import the library as:
from pattern.text.en import suggest

Running Tensor flow from R using rPython

I'm trying to use the Tensor flow Python module in RStudio using the package rPython. I am able to use the tensor flow module directly from Python.
I have a Python file called "run.py" which imports tensor flow, and I can successfully call it in the command line using "python run.py". However, when I use the rPython package and run the following
> python.load("python/run.py")
Error in python.exec(code, get.exception) : No module named tensorflow
Using the following code produces the same error
python.exec("
import sys
sys.argv = ['']
import tensorflow as tf
")
Error in python.exec("\n import sys\n sys.argv = ['']\n import tensorflow as tf\n ") :
No module named tensorflow
Does anyone know why I get this error?

Categories