HELP! I think I'm on mutilprocessing, but I don't know how to end it, noob here

So I was trying to import a document for exercising, and before I wrote down the code below, I was doing another exercise. After I delet the previous code and running the new one, something weird happened.

Here’s what I wrote:

import docx
file_doc = docx.Document(r"C:\Users\94217\Desktop\Essay.docx")  
print('word:' + str(file_doc.paragraphs))

And here’s the result:

C:\Users\94217\Desktop\venv\Scripts\python.exe "C:/Users/94217/Desktop/CODING/py4e/test file.py"
Traceback (most recent call last):
  File "C:\Users\94217\Desktop\venv\lib\site-packages\docx.py", line 21, in <module>
    from PIL import Image
  File "C:\Users\94217\Desktop\venv\lib\site-packages\PIL\Image.py", line 30, in <module>
    import logging
  File "C:\Users\94217\AppData\Local\Programs\Python\Python310\lib\logging\__init__.py", line 28, in <module>
    from string import Template
ImportError: cannot import name 'Template' from 'string' (C:\Users\94217\Desktop\CODING\py4e\string.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\94217\Desktop\CODING\py4e\test file.py", line 1, in <module>
    import docx
  File "C:\Users\94217\Desktop\venv\lib\site-packages\docx.py", line 23, in <module>
    import Image
ModuleNotFoundError: No module named 'Image'
42
42
.ma

I reconized that the number 42 and ‘.ma’ is exactly the output from my previous exercise, I googled it and I think I’m on mutilprocessing?

But I don’t understand how’s that possible considerd I already delete all the code from my last exercise.

Does anyone knows what happened?

Thanks, I really need some help!

If you read the traceback carefully you can see what is happening. You have a Python file in your working directory named string.py. This “shadows” (hides) the Python standard library module of the same name, which is used by dependencies of the docx package you are using. Since your string.py does not provide the expected functionality of the standard library module, an error occurs and the code cannot work correctly.

To fix this, rename your file to something other than the names of the standard library modules and any third-party packages you intend to use. Also, to avoid this in the future, you can always run Python with the -I flag to ignore the current working directory when importing modules (though this will mean that importing other modules of your own local code won’t work until you install it as a Python package, which is recommended and not that hard to do these days. The -P flag in Python 3.11+ will allow doing this more specifically. Also, as a temporary fix, you can simply cd to an empty working directory.

As for the printed lines, I can’t be sure, but those are quite possibly side effects from importing string.py if you had similar code copied there. Putting any top-level imperative code in a main() function and running it if __name__ == "__main__ will avoid this and is likewise a good idea in general.

Yes! That’s exactly what happened! After I rename the ‘string. py’ file and the problem solved! Although I still can’t get the intented output but finally I know where the problem is. (It almost drives me crazy lol)

Thanks! Cheers!

By Timjinlun via Discussions on Python.org at 14Jun2022 05:30:

So I was trying to import a document for exercising, and before I wrote
down the code below, I was doing another exercise. After I delet the
previous code and running the new one, something weird happened.

Here’s what I wrote:

import docx
file_doc = docx.Document(r"C:\Users\94217\Desktop\Essay.docx")
print('word:' + str(file_doc.paragraphs))

And here’s the result:

C:\Users\94217\Desktop\venv\Scripts\python.exe "C:/Users/94217/Desktop/CODING/py4e/test file.py"
Traceback (most recent call last):
 File "C:\Users\94217\Desktop\venv\lib\site-packages\docx.py", line 21, in <module>
   from PIL import Image
 File "C:\Users\94217\Desktop\venv\lib\site-packages\PIL\Image.py", line 30, in <module>
   import logging
 File "C:\Users\94217\AppData\Local\Programs\Python\Python310\lib\logging\__init__.py", line 28, in <module>
   from string import Template
ImportError: cannot import name 'Template' from 'string' (C:\Users\94217\Desktop\CODING\py4e\string.py)

This says that docx is importing PIL.Image which imports logging which
imports the name ‘Template’ from ‘string’. The final line recites the
path to the “string” module, which is in fact your own “string.py” file.

This happens because you named your file with the same name as a
standard python module.

ModuleNotFoundError: No module named ‘Image’
42
42
.ma

I reconized that the number **42** and '**.ma**' is exactly the output 
from my previous exercise, I googled it and I think I'm on 
mutilprocessing?

I have no idea what you found, but search for generic things like small
numbers and short strings is rarely useful, and all results found your
be viewed with scepticism.

Cheers,
Cameron Simpson cs@cskk.id.au

Not sure if you’ll receive this either, but just FYI, you might want to check if Discourse’s email notifications are acting up again—I’m assuming you didn’t receive either my response from several hours ago explaining the same, and @timjinlun confirming that fixed the issue?

Just out of curiosity, as far as I can tell that’s the output from the user’s previous exercise due to being side effects of unintentionally importing the user’s shadowed string module. Nothing to do with multiprocessing of course, as you doubtless inferred as well.

Thanks! After I rename the file and everything go back to normal. But there is still one thing that confused me which is how come I got a result from the exercise I already deleted?

When you import a library file (which is a form of program), Python runs that imported program to pick up the function definitions it contains.

You weren’t executing your programs in parallel. Rather, you were executing them serially. More properly said, you were executing them in a nested manner.

You may have deleted your code from your editor, but it was stored in the strings.py file on your C: drive.

It’s not a problem with the working directory. The problem is using a single directory for scripts, e.g. “C:\Users\94217\Desktop”. When running a script, it’s the directory of the script that gets prepended to sys.path, not the working directory. This is often a good thing for a Python application or suite, but it’s a problem when storing unrelated scripts in a common directory, like the ubiquitous “bin” directories in POSIX.

Isolated mode disables this feature, but at the cost of also ignoring all PYTHON* environment variables (e.g. PYTHONPATH, PYTHONUTF8, PYTHONIOENCODING, etc) and user site packages. The new -P option in 3.11 disables prepending the script directory to sys.path, as well as the working directory.

Ah, right, thanks—I really should have gotten that right, givenI was a big supporter of the new -P option and helped review and write the docs for it, heh! I’m just so used to it being the cwd with python -m which is almost invariably where I encounter such issues (if I’m not already using -I), and in Spyder it was also the cwd even when running scripts and seeing users run into this issue, until we changed our behavior to append it to sys.path instead of inserting it (funny enough, right before commenting here I was reviewing a Spyder PR related to that, so requested due to what people seemed to assume was my “expertise” in said area—oh, how wrong they are…).