AREPL-vscode icon indicating copy to clipboard operation
AREPL-vscode copied to clipboard

ModuleNotFoundError: No module named 'typing.io'; 'typing' is not a package

Open adamgranthendry opened this issue 4 years ago • 6 comments

Bug

OS: Windows 10 Professional x64, Build 1909 Python: 3.8.10 VSCode: 1.58.2 AREPL: 2.0.2

When using importlib.resources, AREPL generates the error:

File "C:\Program Files\Python38\lib\importlib\resources.py", line 13, in <module>
    from typing.io import BinaryIO, TextIO
ModuleNotFoundError: No module named 'typing.io'; 'typing' is not a package

Minimal Example

  1. Create python 3.8.10 virtual environment with venv in project folder:
py -m venv .venv
  1. Create VSCode user settings to point AREPL to virtual environment:
"AREPL.pythonPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe"
  1. Create file demo.py in project folder
  2. Import importlib.resources in demo.py:
import importlib.resources as rsrc
  1. Click cat button for AREPL to run repl

Expected Behavior AREPL uses virtual environment and is able to import importlib.resources

Screenshots image

image

adamgranthendry avatar Jul 24 '21 00:07 adamgranthendry

good find, this is indeed a bug :(

Almenon avatar Jul 26 '21 05:07 Almenon

@Almenon Thank you! Are there any next steps towards fixing this bug?

adamgranthendry avatar Jul 29 '21 19:07 adamgranthendry

I can do some investigation if I had time for it, which I don't. Unless you're interested in taking a look it's likely going to be a while before it will be fixed. If you are interested I can help you out or review a PR.

Almenon avatar Jul 30 '21 05:07 Almenon

If I may, this seems to have been a problem with the resources.py file itself. I fixed it for myself by replacing from typing.io with just from typing. Don't know why typing.io was even there when the classes were in typing. 🤷

Edit: Now I noticed that there is a wrapper class io for the TextIO and BinaryIO classes. Don't know why it doesn't work though, but the fix works for me.

gulash007 avatar Dec 04 '21 21:12 gulash007

If I may, this seems to have been a problem with the resources.py file itself. I fixed it for myself by replacing from typing.io with just from typing. Don't know why typing.io was even there when the classes were in typing. 🤷

Edit: Now I noticed that there is a wrapper class io for the TextIO and BinaryIO classes. Don't know why it doesn't work though, but the fix works for me.

Hello, can you please explain how exactly did you fixed it ? Do i need to do it on VS code. Where can i find the ressource.py file ?

OrhanYaz avatar Apr 26 '22 09:04 OrhanYaz

Hello, can you please explain how exactly did you fixed it ? Do i need to do it on VS code. Where can i find the ressource.py file ?

Sorry for a late reply. For what it's worth, the resources.py file is a part of the importlib library. On my mac it's located at /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/resources.py.

gulash007 avatar Jun 28 '22 08:06 gulash007

Hello, can you please explain how exactly did you fixed it ? Do i need to do it on VS code. Where can i find the ressource.py file ?

Sorry for a late reply. For what it's worth, the resources.py file is a part of the importlib library. On my mac it's located at /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/resources.py.

Hi! For those using Windows, you can find resources.py file into the /users/<your_user>/AppData/Local/Programs/Python/Python310/Lib/importlib/resources.py. As @gulash007 said, you just need to delete ".io" from "typing.io" and that should work.

ggonzalez-elec avatar Oct 08 '22 21:10 ggonzalez-elec

Don't know why typing.io was even there when the classes were in typing. 🤷

@gulash007 typing.io was deprecated in Python 3.8 (see the typing module docs):

image

However, this hasn't been removed yet from importlib (see the cpython source): It isn't scheduled to be removed completely until Python 3.12.

io is made into a namespace within the typing module, but typing is not a package (i.e. a folder with a __init__.py; more specifically, a module with a __path__ attribute). One can import typing.io perfectly fine in a standard REPL.

The above leads me to believe the error is coming from the TypeScript modules concerning the python environment as used in AREPL. It wants to import io as a submodule of package typing when in fact typing.io is a module with the name typing.io (as if someone had written it as the file typing.io.py).

adam-grant-hendry avatar Oct 13 '22 17:10 adam-grant-hendry

Hi! For those using Windows, you can find resources.py file into the /users/<your_user>/AppData/Local/Programs/Python/Python310/Lib/importlib/resources.py

@ggonzalez-elec The bug isn't with resources.py, but with AREPL, so I don't recommend changing resources.py because the problem is deeper than that.

That said, Python is scheduled to change typing.io to typing in Python 3.12, so ultimately the change is harmless. However, every time you install a virtual environment or Python on a new system, you're going to have to remember to do this manual change, which is a pain.

Also be careful: you have a local system install of Python 3.10 and are not using a virtual environment, which is why your path is what it is. This is not what all users will have. importlib.resources is a standard library module, so on Windows it will always exist in

Python<3.x>\Lib\importlib\resources.py

so the problem of finding it boils down to where you have installed Python.

On Windows:

%USERPROFILE% = C:\Users\<user_name> %LOCALAPPDATA% = %USERPROFILE%\AppData\Local %PROGRAMFILES% = C:\Program Files %PROGRAMFILES(X86)% = C:\Program Files (x86)

Local User Install: %LOCALAPPDATA%\Programs\Python\Python<3.x>
System Install (32-bt): %PROGRAMFILES(X86)%\Python<3.x> System Install (64-bit): %PROGRAMFILES%\Python<3.x>

In a virtual environment (typically named .venv), it lives in the folder .venv\Lib\importlib\resources.py.

Again, however, I don't recommend changing resources.py because that doesn't solve the actual problem and will cause you a headache when dealing with multiple environments.

adam-grant-hendry avatar Oct 15 '22 16:10 adam-grant-hendry

If I may, this seems to have been a problem with the resources.py file itself.

This is not a problem with resources.py. typing.io is deprecated and scheduled to be removed, but typing.io can be imported and used in all versions of Python up to 3.11 outside of AREPL just fine.

The issue is with the manner AREPL is trying to import typing.io. typing and typing.io are not packages, as I explained previously. typing.io is a namespace manually added to sys.path from within the typing module.

AREPL is attemping to import typing.io as if io is a submodule of a package typing, which is incorrect. I believe this is related to AREPL's typescript code.

I strongly discourage users from manually changing resources.py. Doing so does not solve the actual problem.

adam-grant-hendry avatar Oct 15 '22 16:10 adam-grant-hendry

I believe this is related to AREPL's typescript code.

The Typescript code doesn't have much to do with the python code outside of spawning the process and pipe I/O. The issue is more likely to be with the AREPL python code, which does some funky stuff around importing so there is a fresh state each time. https://github.com/Almenon/AREPL-backend/blob/master/python/arepl_python_evaluator.py

One thing that's pretty weird is that I don't get this bug with python 3.9, only 3.8 and 3.10 (haven't checked 3.11). I also can't reproduce the issue in a unit test. @adam-grant-hendry do you get this issue with 3.9?

Also, adding "typing.io" to the modules_to_keep variable in C:\Users\<yourUser>\.vscode\extensions\almenon.arepl-2.0.3\node_modules\arepl-backend\python\arepl_python_evaluator.py (insert after line 27) fixes the issue on my end. Does it fix the issue for you?

Almenon avatar Oct 15 '22 18:10 Almenon

I hit this problem today and stumbled upon this conversation. @Almenon I tried what you suggest above here,

Also, adding "typing.io" to the modules_to_keep variable in C:\Users<yourUser>.vscode\extensions\almenon.arepl-2.0.3\node_modules\arepl-backend\python\arepl_python_evaluator.py (insert after line 27) fixes the issue on my end.

It seemed to help. My version of python is 3.10.8

ashwin-interfold avatar Oct 16 '22 17:10 ashwin-interfold

This error should be fixed with the latest release of AREPL (2.0.4)

Almenon avatar Oct 23 '22 21:10 Almenon