Problem in Pickle library in Python

Asked

Viewed 259 times

0

So, trying to apply the following code, but the compiler ta returning me following problem, I can not think of anything that is not some installation problem, but I got this problem in two different computers with the same code:

import pickle

arqT = open('turmaAlunos.dat', 'ab')
choice = 'n'


while True:
    nome = input('Digite nome do aluno')
    pickle.dump(nome,arqT)

    if name == '':
        while True:
            choice = input("Voce quer mesmo terminar? [s/n]")
            if choice == 's':
                break
            elif choice == 'n':
                break
            else:
                print("Opção inválida")
                continue

        if choice == 's':
            break
        else:
            continue

    matOk = False
    while not matOk:
        matricula = input('Digite a matricula do aluno:')
        if len(matricula) < 6:
            print('Matricula inválida, digite novamente')
        else:
            matOk = True

    notas = []

    print("Digite as notas do alunos:\n")
    for i in range(3):
        notas.append(int(input()))

    for i in range(len(notas)):
        notas[i] = int(notas[i])

    pickle.dump(notas,arqT)

arqT.close()

----Return:

/usr/bin/python3.6 /../PycharmProjects/aval1/pickle.py
Digite nome do aluno etc
Traceback (most recent call last):
  File "../PycharmProjects/aval1/pickle.py", line 1, in <module>
    import pickle
  File "../PycharmProjects/aval1/pickle.py", line 9, in <module>
    pickle.dump(nome,arqT)
AttributeError: module 'pickle' has no attribute 'dump'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 26,                in <module>
    import cPickle as pickle
ModuleNotFoundError: No module named 'cPickle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in        apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in     <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in     <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in     <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 36,     in <module>
    import pickle
  File "../PycharmProjects/aval1/pickle.py", line 9, in <module>
    pickle.dump(nome,arqT)
AttributeError: module 'pickle' has no attribute 'dump'

Original exception was:
Traceback (most recent call last):
  File "../PycharmProjects/aval1/pickle.py", line 1, in <module>
    import pickle
  File "../PycharmProjects/aval1/pickle.py", line 9, in <module>
    pickle.dump(nome,arqT)
AttributeError: module 'pickle' has no attribute 'dump'

----att

I still have a problem with the code: import pickle

arq = open('testePickle.dat','wb')

variavel = 0

pickle.dump(variavel,arq)
  • note: linux usage

  • how did you install this Python3? It’s not supposed to happen.

  • Actually, it looks like you have files. py from Python2 on top of your Python3 libs - you may have detracted from your Python installations (in the sense of being unusable) if you ever used "sudo Pip install" - in which case "Pip" installs files from it on top of the system’s Python files, and can happen mixing versions.

  • I never got to install python2 in this distro that I’m using, and I only used Pip in an attempt to fix this problem, I didn’t even have Pip installed before

2 answers

1

Your problem is in the name of your file. You called it pickle.py however pickle already and a module name that comes with python, so it’s getting confused what you mean by import pickle.

Rename your program from pickle.py for test_pickle.py. Also remove the file pickle.pyc that was generated in the same folder. This will fix your problem.

-1

Apparently you broke your Python3 system installation - and possibly Python2.

All mdoernos Linux allow - if no longer by default - the parallel installation of Python 2.7 and a recent version of Python 3. Try to interfere with these installations outside of package management of your Linux distribution, even if it is with the use of "Pip install" - Python’s own package manager - can have destructive effects.

Installing unofficial repositories with different versions of software (for example, an unofficial repository like the ones called "PPA") can sometimes cause more problems than benefits - since the integration of the packages in these repositories with the rest of the system is not curated by the authors of the distribution - in general it contains only what "works for the author of the repository")

pickle is one of the core modules of the standard Python library - and the errors you have show that it cannot be loaded - and for a complex question (cpickle only exists in Python 2, and you have an error message that "cPickle does not exist" in a module that is in a Python 3 folder).

What you have to try to do there is to restore python2 E installations from Python3 to the originals of your Linux distribution - and remember to never try to install anything there except the system package manager. To install other versions of Python, preferably by compiling them yourself, in a different prefix, and for any Python project, create a "virtualenv" that can contain custom versions of all the third-party libraries that the project uses in isolation. (Never use "sudo Pip install" to install or update a system Python package).

  • Thanks, my linux distro came zeroed apps, I installed everything manually, but I will do what you instructed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.