I tried to read pickle file using Anaconda Navigator and have the following script.
import pickle
import sys, os
with open('pickle1', 'rb') as fp:
data_new = pickle.load(fp)
After running the window I get the following error window.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-047bee0f1247> in <module>()
3
4 with open('pickle1', 'rb') as fp:
----> 5 data_new = pickle.load(fp)
ModuleNotFoundError: No module named 'Data'
Can you please help me fix this issue? I tried to rename file to *.pkl, and *.csv formats, but it did not help. Original data file has no extension of its own.
The program that created the pickle file did import Data and there are references to that module inside the pickled object. The program that loads the pickled object needs to be able to import that module to resolve those references. Either put the location of Data.py on your PYTHONPATH (or add the location to sys.path), or copy the module to where your program can find it.
Related
I had a pandas df a while back in 3.10 that I put into a .pkl pickle file. Because one of my modules is only available in 3.6, I had to revert the rest back to 3.6. However, I can't do pandas.read_pickle("file.pkl") now because my older pandas can't handle the newer file.
I'm getting an error like this:
AttributeError: Can't get attribute '_unpickle_block' on <module 'pandas._libs.internals' from '/opt/mambaforge/envs/...
What I tried to do as a workaround is to load the file in the new python 3.10 and then write to another file using PROTOCOL=4 because 4 is the protocol used in python 3.6.
Here's my code:
df = pd.read_pickle('file3ten.pkl')
import pickle5 as pickle
with open(path_to_protocol5, "rb") as fh:
data = pickle.load(fh)
import pickle
pickle.HIGHEST_PROTOCOL=4
with open("file3six.pkl", 'wb') as pfile:
pickle.dump(df, pfile, protocol=pickle.HIGHEST_PROTOCOL)
Now I thought that I would be able to do pd.read_pickle('file3six.pkl') because the protocol was reduced, but I just got a chained error, saying:
AttributeError: module 'pandas._libs.internals' has no attribute '_unpickle_block'
During handling of the above exception, another exception occurred:
...
AttributeError: Can't get attribute '_unpickle_block' on <module 'pandas._libs.internals' from '/opt/mambaforge/envs/
If anyone knows what do to fix this, I'd appreciate it a lot
I am working with phonenumbers module in Python. I am having the issue of circular import. This error omits whenever I run the file from desktop location (C:\Users\AsadA\Desktop). But it raises an error whenever I tried to run this in a particular folder (C:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER ). Please help me!
Sample Code:
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
from phonenumbers import timezone
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))
ERROR:
Traceback (most recent call last):
File "c:/Users/AsadA/Desktop/Python_projects/28-FindingTheNUMBER/phonenumbers.py", line 1, in <module>
import phonenumbers
File "c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py", line 2, in <module>
from phonenumbers import geocoder
ImportError: cannot import name 'geocoder' from partially initialized module 'phonenumbers' (most likely due to a circular import) (c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py)
You might probably named your file as "phonenumber.py". If you are importing something in python, please make sure that file name is not same as imported file. If it is same, then it will create an error.
This happens due to same name conflict with imported file as imported file name is also the same. And if this occur then python always give priority to file with current directory you are working.
So, let say your code is something like below.
import xyz
print(xyz.version)
And your file name is "xyz.py". Python compiler now sees that there are two files of same name "xyz.py", one in script folder where python is installed and another in current directory we are working. So, python compiler compiler choose file to import from current directory you are working on.
So, python read first line import xyz, it imports the file from current directory which means it import again this file and start reading it. In that, again first line is import xyz, then it again imports xyz in current folder causing a loop to occur.
This is called as circular loop.
So, in short, changing your file name can solve the problem.
You are importing the module phonenumbers using 'import phonenumbers' and then you are importing the relevant definitions inside that module in the next few lines. They are redundant.
Fixed code:
import phonenumbers
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(phonenumbers.geocoder.description_for_number(my_Num,'en'))
print(phonenumbers.carrier.name_for_number(my_Num,'en'))
print(phonenumbers.timezone.time_zones_for_number(my_Num))
Or something like this:
from phonenumbers import (
parse,
geocoder,
carrier,
timezone,
)
my_Num=parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))
I am trying to create my own module (mi_modulo.py) and move there all the functions that I have defined in my Jupyter Notebook script, so that it looks cleaner.
However, when I am trying to use these functions that I have already moved to the module, I am not able to use them all, and I get the following message:
module 'mi_modulo' has no attribute 'train4_data_import'
I have installed Anaconda 3.0 and I am running Python 3.7.0 through Jupyter Notebooks. (Forgive me if the expressions sound awkward, I know a bit of Python, but I am not really into all the installation, software, IDE, etc details.)
## mi_modulo.py ##
def train4_data_import(file_name):
df = pandas.read_excel(file_name)
force = df["Signal 1"].values[13:]
acceleration1 = df["Signal 2"].values[13:]
acceleration2 = df["Signal 3"].values[13:]
return force, acceleration1, acceleration2
def hola_mundo():
print("whatever")
## script ##
import pandas
import mi_modulo as mi
mi.hola_mundo()
mi.train4_data_import("Tren4.xlsx")
And this is what I get:
(I was going to show an image but I am not sure how to do that with this stackoverflow new form style)
whatever
AttributeError Traceback (most recent call last)
<ipython-input-18-69a38929f7e6> in <module>()
3 mi.hola_mundo()
4
----> 5 mi.train4_data_import()
AttributeError: module 'mi_modulo' has no attribute 'train4_data_import'
I don't understand why it is able to read one function but not the other.
----------------------------- EDIT 1 ----------------------------
Doing what U9-Forward suggests:
import pandas
from mi_modulo import *
hola_mundo()
train4_data_import("Tren4.xlsx")
I get now the following error:
whatever
NameError Traceback (most recent call last)
<ipython-input-25-e1885200beb7> in <module>()
3 hola_mundo()
4
----> 5 train4_data_import("Tren4.xlsx")
NameError: name 'train4_data_import' is not defined
In jupyter-notebook, sometimes you need to restart the kernel to import all the unsaved module you have. Also, you need to import all the dependency for the custom module within that module.
It is probably because you didn't press Ctrl+S or hit the save button on the file, it will probably work if you do that:
Ctrl+S
Or save button.
then run script.py and see it working :-)
I'm trying to convert Keras to a Core ML model but I'm stuck when converting the Python file into a mlmodel.
I'm getting errors when importing submodules of 'coremltools'.
The error that I'm getting is: "python recog.py
Traceback (most recent call last):
File "recog.py", line 3, in
from coremltools import convert
ImportError: cannot import name 'convert'
"
I tried to import the submodules in a different way but nothing worked for me.
I hope anyone can help me!
You can see the Python code, in the sample below:
import coremltools
from coremltools import converters
from coremltools import convert
coreml_model = coremltools.converters.keras.convert('model.h5', input_names='data', image_input_names='data', is_bgr=True, output_names='species')
coreml_model.save('model.mlmodel')
Make sure the name you are trying to import is in the module coremltools.
In the file, coremtools.py double check if the name is same I.e. convert.
Check the location of the coremtools.py file, is it in the main folder of python where python.exe exists?
I have a function that loads data using the current path like this: open('./filename', 'rb'). When I call it from a module located in the same package, it works, but when I import its package from a module in a different package and call it, I get an error telling me that the path './filename' does not exist. The error is raised by the call to open. What is causing this, and how do I fix this?
I'm not aware of best practices, but modules have a __file__ attribute set to a string representation of the name of the file they were loaded from. Thus, you can do this:
import os.path
# Get the directory this module is being loaded from
module_directory = os.path.dirname(__file__)
# Get the path to the file we want to open
file_path = os.path.join(module_directory, 'filename')
with open(file_path, 'rb') as f:
# do what you want with the file