4

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?

python_user
  • 196
  • 1
  • 5
  • 15
  • Related: https://stackoverflow.com/questions/27150481/pydoc-not-seeing-docstrings – PM 2Ring Dec 02 '19 at 09:03
  • Is your ``config.py`` valid? Does it contain syntax errors or missing dependencies? Can you import it in an application or interactive session? – MisterMiyagi Dec 02 '19 at 09:12

4 Answers4

3

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.

Yoga
  • 61
  • 6
1

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.

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

;)

0

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!

Gilco
  • 1,326
  • 12
  • 13
-1

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.

iFlo
  • 1,442
  • 10
  • 19
  • I am sorry but I did not get it. I am using Python 2.7. So what should I do in mainfile.py to import config.py. Currently I have written `from config import *` as mainfile and config file are on same level. – python_user Jan 17 '17 at 09:56
  • First, you should avoid naming your modules with the same name as standard-library or built-in module names. Second, you must use : `from __future__ import absolute_import` – iFlo Jan 17 '17 at 10:03
  • and third : `from . import config` in your mainfile. You should read this : https://docs.python.org/2.5/whatsnew/pep-328.html . iIt's exactly your issue. – iFlo Jan 17 '17 at 10:04
  • Ok. Now I renamed my config.py file to parameters.py. In mainfile, I am importing it as `from parameters import *` and other file as `from asc import *`. Now when i run my pydoc command I get error as ` : No module named parameters`. Why am I getting this error? and why it does give for asc.py? – python_user Jan 17 '17 at 10:12
  • ** why it does not give for asc.py – python_user Jan 17 '17 at 10:18