NameError: name 'learner' is not defined - from fastai.vision - python

NameError Traceback (most recent call last)
<ipython-input-13-94eb7b0557cb> in <module>
----> 1 from fastai.vision import *
2 import pickle as pkl
3 import builtins
~/anaconda3/envs/FGMLCI/lib/python3.6/site-packages/fastai/vision/__init__.py in <module>
10 from .. import vision
11
---> 12 __all__ = [*basics.__all__, *learner.__all__, *data.__all__, *image.__all__, *transform.__all__, *tta.__all__, 'models', 'vision']
13
NameError: name 'learner' is not defined
--------------------------------------
version :
fastai==1.0.60
torch==1.2.0
torchvision==0.4.0a0+6b959ee

it happened me with fastai version 2, my problem is that I deleted this import by accident
from fastai.vision.all import
just for the ones that have this problem in version 2

Related

ImportError: cannot import name 'is_directory' from 'PIL._util' (/usr/local/lib/python3.7/dist-packages/PIL/_util.py) [duplicate]

This question already has answers here:
ImportError: cannot import name 'is_directory'
(3 answers)
Closed last month.
While using this code, I get this error of Pillow. I tried re-installing pillow but still struggling with this issue. Any help to make this code run?
import layoutparser as lp
model = lp.Detectron2LayoutModel(
config_path ='lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', # In model catalog
label_map ={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, # In model`label_map`
extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] # Optional
)
model.detect(image)
Getting this error:
ImportError Traceback (most recent call last)
[<ipython-input-6-59f0fb07b7e3>](https://localhost:8080/#) in <module>
1 import layoutparser as lp
----> 2 model = lp.Detectron2LayoutModel(
3 config_path ='lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', # In model catalog
4 label_map ={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, # In model`label_map`
5 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] # Optional
31 frames
[/usr/local/lib/python3.7/dist-packages/PIL/ImageFont.py](https://localhost:8080/#) in <module>
35 from . import Image
36 from ._deprecate import deprecate
---> 37 from ._util import is_directory, is_path
38
39
ImportError: cannot import name 'is_directory' from 'PIL._util' (/usr/local/lib/python3.7/dist-packages/PIL/_util.py)
Run the below command before installing the library:
!pip install fastcore -U

Importing a .tif into Jupyter Notebook using python script

I have been trying to import a .tif into python using the following code:
image = 'location/map_Western_Cape.tif' #use same location as above
geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))
image_Gauteng = 'location/map_Gauteng.tif' #use same location as above
geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))
However, I keep getting this error message:
AttributeError Traceback (most recent call last)
Input In [11], in <cell line: 4>()
1 # Load the .tif from local machine
3 image = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Western_Cape.tif' #use same location as above
----> 4 geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))
6 image_Gauteng = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Gauteng.tif' #use same location as above
7 geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))
File ~\anaconda3\lib\site-packages\geemap\common.py:12559, in plot_raster(image, band, cmap, proj, figsize, open_kwargs, **kwargs)
12557 try:
12558 import pvxarray
> 12559 import rioxarray
12560 import xarray
12561 except ImportError:
File ~\anaconda3\lib\site-packages\rioxarray\__init__.py:6, in <module>
3 __author__ = """rioxarray Contributors"""
4 __email__ = "alansnow21#gmail.com"
----> 6 import rioxarray.raster_array # noqa
7 import rioxarray.raster_dataset # noqa
8 from rioxarray._io import open_rasterio # noqa
File ~\anaconda3\lib\site-packages\rioxarray\raster_array.py:28, in <module>
14 from typing import (
15 Any,
16 Dict,
(...)
24 Union,
25 )
27 import numpy as np
---> 28 import rasterio
29 import rasterio.mask
30 import rasterio.warp
File ~\anaconda3\lib\site-packages\rasterio\__init__.py:44, in <module>
41 import warnings
43 import rasterio._loading
---> 44 with rasterio._loading.add_gdal_dll_directories():
45 from rasterio._show_versions import show_versions
46 from rasterio._version import gdal_version, get_geos_version, get_proj_version
AttributeError: partially initialized module 'rasterio' has no attribute '_loading' (most likely due to a circular import)
I have tried installing all of the listed modules, but that ended up getting a bunch of errors too.
Could anyone recommend a way to import these tifs?

ImportError: cannot import name '_LazyModule' from 'transformers.utils'

I am trying to run a text summarization "t5-base" model. The code used to work when I first ran it but after installing/reinstalling some packages, it no longer works. Can anyone please tell me how to resolve this issue? 😭
Here is my code:
import torch
from transformers import AutoModel, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('t5-base')
model = AutoModelWithLMHead.from_pretrained('t5-base', return_dict=True)
inputs = tokenizer.encode("summarize: " + text,
return_tensors='pt',
max_length=512,
truncation=True)
summary_ids = model.generate(inputs, max_length=150, min_length=80, length_penalty=5., num_beams=2)
text = tokenizer.decode(summary_ids[0])
text = text.replace("<pad>","").replace("</s>","")
text
Below is the error message I get:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-46-2c9eeafa599f> in <module>
1 import torch
----> 2 from transformers import AutoModel, AutoTokenizer
~/opt/anaconda3/lib/python3.7/site-packages/transformers/__init__.py in <module>
29 # Check the dependencies satisfy the minimal versions required.
30 from . import dependency_versions_check
---> 31 from .utils import (
32 _LazyModule,
33 is_flax_available,
ImportError: cannot import name '_LazyModule' from 'transformers.utils' (/Users/sangjinlee/opt/anaconda3/lib/python3.7/site-packages/transformers/utils/__init__.py)

Is there a way to have a "master list" of classes, and use that list within those classes?

I have a series of Python modules. Each one contains a class with a name matching its directory:
a/__init__.py
a/aa/__init__.py
a/ab/__init__.py
b/ba/__init__.py
b/bb/__init__.py
c/ca/__init__.py
c/ca/caa/__init__.py
utils/__init__.py
I have a free variable and function that maps the class in each module to a string, in utils/__init__.py:
import a
import a.aa
import a.ab
import b
import b.ba
import b.bb
import c
import c.ca
import c.ca.caa
MAPPING = {
"Foobar": a.A,
"Apple": a.aa.Aa,
"Banana": a.ab.Ab,
"Clementine": b.ba.Ba,
"Granola": b.B,
"Donut": b.bb.Bb,
"Hashbrowns": c.C,
"Egg": c.ca.Ca,
"Furniture": c.ca.caa.Caa,
}
def find_class(s):
return MAPPING[s]
However, I would like to use this function inside some of these classes. For example, b/ba/__init__.py may contain the following:
import utils
class Ba():
def mycall(self, s):
return utils.find_class(s)
This causes an issue trying to import that class:
Python 3.9.6 (default, Jun 29 2021, 05:25:02)
>>> from b.ba import Ba
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3imports/b/ba/__init__.py", line 1, in <module>
import utils
File ".../python3imports/utils/__init__.py", line 15, in <module>
"Clementine": b.ba.Ba,
AttributeError: module 'b' has no attribute 'ba'
>>>
Is there a way to have a "master list" of classes, and use that list within those classes?
It's a little hacky but this solution is functional:
MAPPING = {}
def _make_mapping(m):
import a
import a.aa
import a.ab
import b
import b.ba
import b.bb
import c
import c.ca
import c.ca.caa
m["Foobar"] = a.A
m["Apple"] = a.aa.Aa
m["Banana"] = a.ab.Ab
m["Clementine"] = b.ba.Ba
m["Granola"] = b.B
m["Donut"] = b.bb.Bb
m["Hashbrowns"] = c.C
m["Egg"] = c.ca.Ca
m["Furniture"] = c.ca.caa.Caa
def find_class(s):
if not MAPPING:
_make_mapping(MAPPING)
return MAPPING[s]
This prevents any class imports from being attempted until find_class is run for the first time, determines that MAPPING is empty, and populates it. In this way, importing utils in one of the listed classes keeps a circular import from occurring.

After importing a module (func1.py) into the test.py file, getting nameError: name ‘pd’ is not defined

import sys
sys.path.append('/Users/name/Documents/pythontemp/functemp/')
In the test.py file:
import pandas as pd
for file in os.listdir('/Users/name/Documents/pythontemp/functemp/'):
if 'func' not in file:
continue
fileN=file.split('.')[0]
print(fileN)
Class=__import__(fileN)
func=getattr(Class,fileN)
para=func.__code__.co_varnames
print(para)
if 'n1' not in para:
idx=func.__code__.co_argcount
else:
idx=para.index('n1')
print(idx)
paras=[eval(x) for x in para[:(idx)]]
#print(paras)
dict_=dict(zip(para[:idx],paras))
#print(dict_)
factor=func(**dict_)
display(factor)
The nameError is:
func2
('a', 'b', 'c', 'result')
3
NameError
Traceback (most recent call last)
<ipython-input-9-4b45b006dea5> in <module>
22 #print(dict_)
23
---> 24 factor=func(**dict_)
25 display(factor)
~/Documents/pythontemp/functemp/func2.py in func2(a, b, c)
1 def func2(a,b,c):
----> 2 a=pd.DataFrame(a)
3 result=a+b-c
4 return result
NameError: name 'pd' is not defined
The func2.py file is:
def func2(a,b,c):
a=pd.DataFrame(a)
result=a+b-c
return result
I have lots of py file like func2.py(eg.func1.py, func3.py...) in the same file folder.
In the test.py, I have imported pd, but func2.py not.
So how to solve it?
The NameError in func2.py is because that file does not know what pd is because you have not imported it in that file. Note that you have correctly imported pandas in the test.py file. Add the following to the top each of the func*.py files that have pd:
import pandas as pd

Categories

Resources