ImportError: cannot import name Lock - python-3.x

I finished the code yesterday and ran it well. But after some other process on another code document, the previous code doesn't work. What funny is, as I learned from some answers on the website, if I change the wheel import sequence, the error result will be different.
I have tried reinstalled python. But it doesn't work.
Code 1:
import numpy as np
import scipy.integrate
import pandas as pd
print(np.mean(1+3))
Code 2:
import pandas as pd
import numpy as np
import scipy.integrate
print(np.mean(1+3))

Related

How to load Keras model from a database (which is essentially not a .h5 file)?

I have created a python script to pull weights information from database and now I want to load it as Keras.model , but script is generating error.
It is working perfectly fine when I am using file instead of extracting the data from database
This works perfectly fine if i use model_weights instead of model_weights_1.
In database this is stored as binary blob.
from keras.models import model_from_json
import tensorflow as tf
import pyodbc
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import configparser
from skimage.transform import resize
import matplotlib.pyplot as plt
import os
from io import StringIO,BytesIO
import h5py
model_struct=f'location of json file'
model_weights=f'location of weight file'
model_weights_1,=cursor.execute(("select query to get weights").fetchone()
model_struct_str,=cursor.execute(("select query to get json string").fetchone()
mdl = model_from_json(model_struct_str)
mdl.load_weights(model_weights_1)
It is throwing error
filepath.endswith(".h5")
TypeError: endswith first arg must be bytes or a tuple of bytes, not str
As Load model is expecting a file which ends with .h5
I also tried to create h5 file but i am unable to do that from binary blob.
Any help will be appreciated

"Import as" isn't recognized, normal import is

I'm playing around with matplotlib to understand its structure better and I'm confused by the following piece of code:
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot # AttributeError: module 'matplotlib' has no attribute 'pyplot'
If on the other hand I abstain from importing matplotlib as a different name and execute instead
import matplotlib
from matplotlib import pyplot as plt #works!
the things work.
Even more crazy, if I "combine" these two
import matplotlib as mpl
from matplotlib import pyplot as plt #works!
mpl.pyplot.get_backend() # works
I can curiously access attributes from pyplot even if I reference it as mpl.pyplot.
What is going on here, why does
from mpl import pyplot as plt throws a ModuleNotFoundError?
import mpl.pyplot not work? Since the error message indcates that mpl was correctly resolved to matplotlib, yet still pyplot couldn't be found...
referencing pyplot as mpl.pyplot not cause an error in my last example?
(Note that I do know of course that the preferred way is to import pyplot as import matplotlib.pyplot as plt but the point of my investigation is precisely to understand what fails and why when I ventured outside the welltrodden streets of code.)
The as part in an import statement is just syntactic sugar for assigning the imported module to a variable with the given name. The import documentation describes it as such:
If the module name is followed by as, then the name following as is bound directly to the imported module.
"Bound" in this context means doing an assignment to the given name. The following statement
import someModule as someName
Is equivalent to this code:
import someModule
someName = someModule
So, when you do import matplotlib as mpl, all you do is create a variable called mpl. This has no effect on any further import statements, as import statements do not care about your local variables, but search for python packages and modules - and an import as statement cannot change the package or module name of the imported element. Which is a good thing, as you do not want your import statements to fail just because another import 5 lines earlier used a conflicting name in an as clause.
Now, why you're getting weird results with the import mpl.pyplot statement - no idea, it shouldn't work. You can see the expected behaviour if you try the following:
import os as asdf
import asdf.path #ModuleNotFoundError: no module named 'asdf'
Tested with python 3.10.2 on Archlinux. If your example is reproducible, then you might have found a weird bug or undefined behaviour in your specific python version, or have some other issue (e.g. a mpl module in your path... although that on its own wouldn't explain why you get an error message about matplotlib instead of mpl).
In conclusion, all as does is assigning a name to the imported object, so a name assigned with as cannot be used as a source in another import statement.
On package imports and why matplotlib.pyplot gives an error:
Importing a package only imports the package itself, not any of its subpackages or modules. A package can explicitly import submodules in its __init__.py, so if the matplotlib init file would contain a statement like from . import pyplot line then accessing matplotlib.pyplot would work. There are however many reasons why a package might choose not to import any submodules, such as time and resources required to import and initialize them.
Everything in python is a object, but when you import matplotlib you are importing all library but you cannot change your functions names, the library will looking for plt
from matplotlib import pyplot as plt in this case.
You can use plt directly but you cannot use mpl.plt.
When you tried it :
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot
You should import correctly:
import matplotlib as mpl
from mpl import pyplot
mpl.pyplot
'As' change the module name what you going to use in your project, but not from another import. But if you use plt directly it is going works.
Try this code to understand :
import matplotlib as mpl
import matplotlib
print(type(matplotlib))
print(type(mpl))
from matplotlib import pyplot as plt# ModuleNotFoundError : No module named 'mpl'
from matplotlib import pyplot
print(type(plt))
print(type(mpl.pyplot))
plt
mpl.pyplot
When you do import module as mdl, it doesn't change the module name. It only affects other statements when you actually use it. But the name doesn't change in the actual python lib or the external library. So what you can do is this:
import matplotlib
from matplotlib import pyplot as plt
or just from matplotlib import pyplot as plt
This is my answer...

ImportError: cannot import name 'PCA' from 'matplotlib.mlab'

According to this task:
Principal Component Analysis (PCA) in Python
I included this line
import from matplotlib.mlab import PCA
but I get the error message:
cannot import name 'PCA' from 'matplotlib.mlab'
I'm using Python3.7 and I have no idea how I can use the PCA function from matlab. Is the new version of matplotlib depricated or is PCA included to another library?
I really don't know if it is too late to reply now. But I will just place it here anyways.
import numpy as np
from sklearn.decomposition import PCA

HDFStore initialization error: name is not defined

When I initialize HDFStore
import numpy as np
import pandas as pd
hdf = pd.HDFStore('polar.h5')
I see an error in flavor.py:
NameError, name '_conv_python_to_python' is not defined
I am using anaconda and pandas does work for dataframe stuff
I resolved it by adding:
import h5py
from pandas import HDFStore,DataFrame
This is actually not a real problem. I checked Breakpoint: "All Exceptions" and it paused here, simply unchecking it resolves the issue. You can continue from this error.

i am trying to take shot from ipcamera and open as opencv and convert to numpy formate

use IP Webcam with opencv as a wireless camera
i fallowed this steps
https://thecodacus.com/ip-webcam-opencv-wireless-camera/#.Ws-oS53hXVM
But i need for python3
i tried my best to do
here my code
import urllib.request
import cv2
import numpy as np
url='http://192.168.43.1:8080/shot.jpg'
while True:
x=urllib.request.urlopen(url)
imgNp=np.array(bytearray(x.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
cv2.imshow('test',img)
if ord('q')==cv2.waitKey(10):
exit(0)
But it gives error message
File "ipcam.py", line 8
x=urllib.request.urlopen(url)
^
IndentationError: expected an indented block
I am not Good at python3 help me please
Python Is Sensitive To Indentation..
Its equivalent to {} in C, C++, etc, Think of it as a block of codes to be executed..
import urllib.request
import cv2
import numpy as np
url='http://192.168.43.1:8080/shot.jpg'
while True:
x=urllib.request.urlopen(url)
imgNp=np.array(bytearray(x.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
cv2.imshow('test',img)
if ord('q')==cv2.waitKey(10):
exit(0)

Resources