MrJman006
MrJman006

Reputation: 770

Python 3 Import Module Not Working According To Docs

So I usually work with Python 2.7.x, but I decided to play around with Python 3 today and see if I want to move up. I downloaded and installed Python 3.6.2 and so far I am not convinced!

So I am trying to import some code from another Python script/module residing in the same directory. Below is an example.

Directory Structure

WorkingDir
|-- main.py
|-- module_a.py

main.py

import os
import module_a

if(__name__ == "__main__"):
    module_a.sayHi()

module_a.py

def sayHi():
    print("Hi There!")

If I run the following:

python main.py

I get the following error:

Traceback (most recent call last):
    File "main.py", line 2, in <module>
        import module_a
ModuleNotFoundError: No module named 'module_a'

Reading the documentation found here: https://docs.python.org/3/tutorial/modules.html

6.1.2. The Module Search Path

When a module named spam is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named spam.py in a list of directories given by the variable
sys.path. sys.path is initialized from these locations:

    The directory containing the input script (or the current directory
    when no file is specified).

    PYTHONPATH (a list of directory names, with the same syntax as the
    shell variable PATH).

    The installation-dependent default.

So I also tried setting PYTHONPATH, but still get the error. I have also tried various other formats such as from ... import ... with no success. Am I missing something or is the documentation faulty?

Edit:

Some users have pointed out that I should include __init__.py in WorkingDir. I have tried that and it does not work for me. In addition, the documentation makes it sound like I no longer am required to have the __init__.py file to make imports work and I would prefer, for this particular test, to avoid them if possible.

I also wanted to say that I tried the exact same files with no changes using Python 2.7.13 and everything works as expected both with and without __init__.py in WorkingDir.

Update:

As one of the commenters was not able to reproduce the issue, I tried running the Python 3 version he/she was using (Python 3.5.2) and I did not get an error. Using that version, everything worked as expected. I believe this is a bug in Python 3.6.2 and have filed a bug report over at the Python bug tracker: http://bugs.python.org/issue31056

Upvotes: 0

Views: 1570

Answers (2)

MrJman006
MrJman006

Reputation: 770

Python 3 works as explained in the documentation. The issue was I downloaded the "embedded" Python package which is available since Python 3.5 according to the documentation. https://docs.python.org/3.6/using/windows.html#embedded-distribution

In the "Finding Modules" section of Python 3 documentation: https://docs.python.org/3.6/using/windows.html#finding-modules, there is a sentence that I missed that was causing the import error.

To completely override sys.path, create a ._pth file with the same 
name as the DLL (python36._pth) or the executable (python._pth) and
specify one line for each path to add to sys.path. The file based on
the DLL name overrides the one based on the executable, which allows 
paths to be restricted for any program loading the runtime if desired.

When the file exists, all registry and environment variables are 
ignored, isolated mode is enabled, and site is not imported unless one
line in the file specifies import site. Blank paths and lines starting
with # are ignored. Each path may be absolute or relative to the
location of the file. Import statements other than to site are not
permitted, and arbitrary code cannot be specified.

A standard Python installation does not have the ._pth file making the sample work as expected. Also removing the ._pth file from the embedded distribution makes the sample I posted work. Although the embedded distribution is for specific purposes and should only be used according to the Python documentation linked above.

Upvotes: 3

Boshika Tara
Boshika Tara

Reputation: 274

It works for me. Here is a link https://trinket.io/python/c444397102

Upvotes: 0

Related Questions