module 'pickle' has no attribute 'dump' - python

import pickle
imelda = ('More Mayhem',
'IMelda May',
'2011',
((1, 'Pulling the Rug'),
(2, 'Psycho'),
(3, 'Mayhem'),
(4, 'Kentish Town Waltz')))
with open("imelda.pickle", "wb") as pickle_file:
pickle.dump(imelda, pickle_file)
I am trying to execute this code, but the console keeps telling me:
module 'pickle' has no attribute 'dump'
Do I have to install pickle via pip? I am not sure what is happening here.

Happened to me too. I had a file called pickle.py in my current directory. Just rename it and it's fixed :)

You might have used your Python file name as pickle.py. Python interpreter is confused and looking for dump function in you file pickle.py instead of the package you have imported. Change the name of your file to something else, it will work.

just check your file name.
if it is pickle.py, then I recommend you to change your file name. instead rename as pickle1.py
it will work.

I don't think its a file name problem. You wrote dumps as dump. Try it with dumps.

If you have file name is pickle.py and any files also saved for this name like pickle.py then remove it first. Then after,save file with other name like pikkle.py instead of pickle.py. your code will be execute.

Related

python. TypeError when extracting rar files: 'NoneType' object is not iterable

I'm trying to extract a rar file using the python library rarfile.RarFile (on windows),
but it keeps giving me
TypeError: 'NoneType' object is not iterable
import rarfile
rarfile=r"E:\rarFiles\CH6ED-(09_14_2021 Tue-10_10_33.99).rar"
with rarfile.RarFile(rarfile) as file:
file.extractall(path="directory",pwd="password")
can anyone please help me get rid of with this error?
I changed my code to this
import rarfile
with rarfile.RarFile(r"E:\rarFiles\CH6ED-(09_14_2021 Tue-10_10_33.99).rar") as file:
file.extractall(path="C:\Users\Joanna\Desktop",pwd="123")
but I gave me the same error
When you do rarfile=r"E:\folder\myrarfile" you are overwriting the name rarfile that was associated with the module you imported, and now is pointing to the string object r"E:\folder\myrarfile". You have to name your variable that holds the path to the .rar file to something else. As a general advice, don't name your variables the same name used for imports.
try
rarfile.RarFile(
r"E:\rarFiles\CH6ED-(09_14_2021 Tue-10_10_33.99).rar"
).extractall(path="C:\Users\Joanna\Desktop",pwd="123")

Is it possible to get the path of a tempfile in Python 3

I was wondering if it was possible to get the file path of a temporary file made using the tempfile library. Basically, I'm trying to make a function that intakes some data, and generates a temporary csv file based off of said data. I was wondering if there was a way to get the path of this temporary file?
Use tempfile.NamedTemporaryFile to create a temporary file with a name, and then use the .name attribute of the object.
Note that there are platform-specific limitations on how this name can be used. The documentation says:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
tempfile.NamedTemporaryFile has a .dir property which will give you want you want.
EDIT: No, it is not .name, #Barmar, but looking through the source code for tempfile, I don't see a .dir property either. However, you can use the .name property in conjunction with os.path's dirname method as follows:
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf:
tf_directory = os.path.dirname(tf.name)
This works just fine to get the full path with the filename
file = tempfile.NamedTemporaryFile()
filename = file.name
The output:
/tmp/tmp_sl4v9ps
Anyway, if you need the path of the tempfile directory you can use tempfile.gettempdir()

for _name in node: TypeError: 'NoneType' object is not iterable

I am using openpyxl python excel reader and writer in my Ubuntu Server
When I use following command
from openpyxl import load_workbook,Workbook
book = load_workbook(filename='/var/www/test.xlsx')
throwing error as
for _name in node:
TypeError: 'NoneType' object is not iterable
but everything fine in local system. Any one know why this is happening and how to solve this?
Update
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 202, in load_workbook
parsed_styles = read_style_table(archive)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 181, in read_style_table
p.parse()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 53, in parse
self.parse_named_styles()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 109, in parse_named_styles
names = self._parse_style_names()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 128, in _parse_style_names
for _name in node:
TypeError: 'NoneType' object is not iterable
It looks like openpyxl isn't able to understand your list of named styles. You should first extract the .xlsx file (it's but a .zip archive with a different ending), and look for the XML file that defines styles (it's xl/styles.xml). Verify it's well-formed XML, and not corrupted. You can do that with one of the many online XML checkers.
If that's not the culprit, you might just try to rely on openpyxl's "benevolence", delete the styles.xml, re-compress to a .zip, rename to .xlsx, and try again.
I had the same problem, and it appeared that the path was incorrect. Open the file with Excel, and copy-paste the path from the "File" tab.
In many cases it has to do with malformed style sheets, as previous posters have already mentioned. I have a few .xlsx files generated by Oracle SQLDeveloper.
When I want to parse them with openpyxl I need to open and then save them in Excel. Otherwise I get the same error as you did.
I did not compare the style sheets (before and after re-saving the file), so I can't tell you what's being fixed by saving the file in Excel. It just works in this particular case, but it may work in yours as well.
This is a bug in openpyxml version 2.3, see: https://bitbucket.org/openpyxl/openpyxl/issues/544, it was fixed in 2.3.1.
Problem can be easily solved by upgrading to the newest version, run:
pip install openpyxl -I
This way pip ignores your current library version and gets the latest one.
I ran into this problem today on Windows.
It turned out it was because while saving the file, under "Save as type: " I selected Strict Open XML Spreadsheet (*.xlsx).
It worked when I saved it as Excel Workbook (*.xlsx)
I had assumed all .xlsx files were the same, this is apparently not the case.

Python: import a file from another directory

I am working in a set of directories with the following structure:
Master/
Subfolder_1/file_1_1.py
file_1_2.txt
Subfolder_2/file_2_1.py
I import file_1_1 in file_2_1 as follows:
import sys
sys.path.append('../file_1_1')
file_1_1 is reading file_1_2.txt which is in the same directory. However, when I call the function that reads file_1_2.txt from file_2_1.py, it says no such file and directory and it gives me the path of file_1_2.txt as:
Master/Subfolder_2/file_1_2.txt
which is a wrong path. It seems like python in this case is using the working directory as a reference. How can I solve this error given that I don't want to include the absolute path for each file I read.
Don't mess with sys.path, and don't think of imports as working against files and directories. Ultimately everything has to live in a file somewhere, but the module hierarchy is a little more subtle than "replace dot with slash and stick a .py at the end".
You almost certainly want to be in Master and run python -m Subfolder_1.file_1_1. You can use pkg_resources to get the text file:
pkg_resources.resource_string('Subfolder_1', 'file_1_1.txt')
info=[]
with open(os.path.realpath('yourtextfile.txt','r') as myfile:
for line in myfile:
info.append(line)

Dump Contents of Python Module loaded in memory

I ran the Python REPL tool and imported a Python Module. Can I can dump the contents of that Module into a file? Is this possible? Thanks in advance.
In what format do you want to write the file? If you want exactly the same format that got imported, that's not hard -- but basically it's done with a file-to-file copy. For example, if the module in question is called blah, you can do:
>>> import shutil
>>> shutil.copy(blah.__file__, '/tmp/blahblah.pyc')
Do you mean something like this?
http://www.datamech.com/devan/trypython/trypython.py
I don't think it is possible, as this is a very restricted environment.
The __file__ attribute is faked, so doesn't map to a real file
You might get a start by getting a reference to your module object:
modobject = __import__("modulename")
Unfortunately those aren't pickleable. You might be able to iterate over dir(modobject) and get some good info out catching errors along the way... or is a string representation of dir(modobject) itself what you want?

Categories

Resources