Search code examples
python-3.xshellpython-docx

Python3.7: Shell restarts whenever I import docx


I'm new to Python. I just installed module python-docx successfully in the command line. But when I typed "import docx" in Shell, Shell always restarts. Then, when I entered "docx.Document()", error occurs as below:

NameError: name "docx" is not defined.

Why this would happen? How to fix? Thanks!

Plus: On the top of my Shell, it shows:

Python 3.7.0a2 (v3.7.0a2:f7ac4fe, Oct 17 2017, 17:06:29) [MSC v.1900 64 bit (AMD64)] on win32

And, the lxml module I first installed is lxml-4.1.1-cp37-cp37m-win_amd64

But, if Python 3.7 hasn't been released yet, does it mean the version I'm using is just Python 3.7 in development?

Thanks!


Solution

  • There's a significant chance this actually has to do with the lxml module, which python-docx is based on. Instead of typing import docx, try import lxml and see if you get the same behavior. If so, that narrows it down a little and you can search on lxml shell restart or similar to get more specific answers.

    lxml uses a couple of DLLs that may not be installed by default on Windows and that can sometimes cause trouble.

    If that doesn't work, you should describe exactly how you installed Python. There are several distributions out there (like Anaconda vs. a "direct" distribution) and that will impact the diagnosis.

    In any case, using Python 3.7 is not advisable for a beginner. What you have is an alpha release which won't be final until next June. Better to stick with what's stable, especially while you're learning. If you can uninstall and find a 3.6 version to install it might "just work", and in any case is likely to produce a more useful error message.