Failed to import dataclasses module
Asked Answered
P

2

15

Today I installed python 3.7 from apt-get to try out the new dataclasses module. I installed it seperately because python3.6 wasn't upgradeable to 3.7.

When I type: python3.7 --version, it gives me: >>> Python 3.7.0a2 as my current version.

The problem is that I can't seem to import dataclasses.

my import statement is: from dataclasses import dataclass as instructed here

This is the error message it's giving me:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'
Error in sys.excepthook:
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 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'

I even tried installing the dataclasses module with pip3: pip3 install dataclasses. Not sure if that's necessary though.

Any suggestion on what might be the problem?

[EDIT] Just tried it with a python3.6 console and it worked fine .. weird.

Protozoal answered 3/7, 2018 at 23:1 Comment(1)
The pip package dataclasses is a backport for Python 3.6. Maybe this explains why it worked fine when you tried with Python 3.6Bibbs
D
6

As suggested by @wim python3.7 -m venv venv_dir

This command will:

  1. Use python3.7 to run the command
  2. The -m flag tells the interpreter to run the next argument as a script
  3. venv is a module, and because of the -m flag it will be run as a script
  4. Finally, the venv_dir is given to the venv module as an argument which this module will use to create a virtual environment directory at

Once this command is run now you'll have a nice sandbox for messing around/testing the dataclasses module.

  • To activate this virtual environment be sure to run source venv_dir/bin/activate before you begin. This command will run the script at venv_dir/bin/activate to set up the necessary environment variables and other things for you
  • To deactivate, simply run deactivate after activating
Devondevona answered 3/7, 2018 at 23:21 Comment(2)
Better: python3.7 -m venv venv_dirMuckraker
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.Dorisdorisa
L
0

I had this problem because my filename was ast.py, which apparently conflicts with some internal module. After renaming the file it works fine.

Literati answered 25/4, 2024 at 16:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.