python_user
python_user

Reputation: 186

Import Error on using pydoc

I have following files in same folder: config.py, init.py, asc.py and mainfile.py

contents of init.py file:

from mainfile import *

config and asc files are imported in mainfile.py.

I am creating html using pydoc as: python -m pydoc -w mainfile.py

I am getting error as: <type 'exceptions.ImportError'>: No module named config

If i will remove config, it works fine and html gets created but does not work with config file.

What is the problem?

Upvotes: 4

Views: 9861

Answers (4)

Gilco
Gilco

Reputation: 1336

pydoc and pdoc read your code!!!

if you will run it from the same directory pdoc --html . or pydoc -w . it should work if all the modules are in the same directory. but if they are not:

make sure your main module in each directory has it sys full path append to it (to the same directory).

sys.path.append("D:/Coding/project/....)

Relative path will not do the trick!

Upvotes: 0

Muneeb Ahmad Khurram
Muneeb Ahmad Khurram

Reputation: 680

In Python3

In some cases in python3.x such as in MacOS people are typing pydoc whereas it must be pydoc3 for Python3.x Versions. Thus, the Syntax in its most basic format is

Basic Syntax

$ pydoc3 -w <module_name> <path_to_module>

This should create a .html file in the current directory in which you are using the terminal in.

///////////////////////////////////////////////////////////////////////////////\

;)

Upvotes: 1

Yoga
Yoga

Reputation: 61

When you get the import error from Pydoc, there are some steps you can find out the bug.

Step

  1. Check the Python version of your files/packages and Pydoc are mapping. Some environment exist Python2 and Python3 at the same time. Basically, the alias pydoc is for Python 2, and pydoc3.X is for Python 3.

  2. Check the import file exists. Sometime you just import nonexistent file/module.

  3. Check the file/module can be imported by python. If you are documentizing your custom module in Python 2, please check __init__.py exist in your every directory of the main source code.

Upvotes: 3

iFlo
iFlo

Reputation: 1484

Short Answer

You need to import config using an absolute import in mainfile.py. Either use:

from yournamefolder import config

or use:

from . import config

Python 3 only supports absolute imports; the statement import config only imports a top level module config

Long Answer

For a more complete solution, see this (and if you are using Python2) :

You will want to read about Absolute and Relative Imports which addresses this very problem. Use:

from __future__ import absolute_import

Using that, any unadorned package name will always refer to the top level package. You will then need to use relative imports (from .email import ...) to access your own package.

Upvotes: -1

Related Questions