Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: Python 3.6 does not find modules while Python 3.5 does

I wanted to upgrade my python version from 3.5 to 3.6. Since I am using WinPython, I have downloaded and installed the recent version just as I did it before with version 3.5.

However, if I use version 3.6 I get a ModuleNotFoundError whenever I import a self-created module. A minimal example: I created a file t1.py that contains only a pass statement and a file t2.py containing the following code:

import t1
print("done")

Both files are in the same folder D:\MyProject\src. Now when I run the file with python 3.5, everything works fine:

'C:\Program Files\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\python.exe' D:\MyProject\src\t2.py
done

However, with python 3.6 I get

'C:\Program Files\WinPython-64bit-3.6.0.1Qt5\python-3.6.0.amd64\python.exe' D:\MyProject\src\t2.py
Traceback (most recent call last):
  File "D:\MyProject\src\t2.py", line 6, in <module>
    import t1
ModuleNotFoundError: No module named 't1'

I ran out of ideas what the issue could be and would appreciate new inspiration.

like image 769
Samufi Avatar asked Feb 16 '17 02:02

Samufi


People also ask

Why can't Python find my modules?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do you fix ModuleNotFoundError and ImportError?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

What is import * in Python?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way.


1 Answers

Would this work ? in t2.py

import os
__path__=[os.path.dirname(os.path.abspath(__file__))]
from . import t1
print("t2 done")

Python-3.6 changes its way of working, with the "python._pth" file next to python.exe (instead of "pyvenv.cfg" in previous versions)

If you don't want to modify your source, then you have to add "D:\MyProject\src" line in Python._pth file, or a relative path to it from python._pth location. in my example, it works with:

python36.zip
DLLs
Lib
.
..\test
import site

"http://bugs.python.org/issue29578?@ok_message=msg%20287921%20created%0Aissue%2029578%20message_count%2C%20messages%20edited%20ok&@template=item"

Other, simpler solution if you have no system-installed python: rename the "python._pth" file, next to "python.exe", as "pythonzz._pth"

The Python "Windows" maintainer just wrote that the simpler solution should be ok also with Python-3.6.0.

like image 192
stonebig Avatar answered Nov 03 '22 02:11

stonebig