I found the solution to my issue, which I believe was a misunderstanding of PEP 420 -- Implicit Namespace Packages.

This page states that, since Python 3.3:

While looking for a module or package named "foo", for each directory in the parent path:

  • If <directory>/foo/__init__.py is found, a regular package is imported and returned.
  • If not, but <directory>/foo is found and is a directory, it is recorded and the scan continues with the next directory in the parent path.

If the scan completes without returning a module or package, and at least one directory was recorded, then a namespace package is created. The new namespace package:

  • Has a __path__ attribute set to an iterable of the path strings that were found and recorded during the scan.
  • Does not have a __file__ attribute.

As defined in the pkg_resources documentation:

A namespace package is a package that only contains other packages and modules, with no direct contents of its own.

My package, as I described it, fell into that category, which was not intended.

After adding an empty __init.py__ below the myProject/ directory I see this happening during the install step:

❯ python setup.py build    
running build
running build_py
creating build/lib
creating build/lib/myProject
copying myProject/__init__.py -> build/lib/myProject
copying myProject/cli.py -> build/lib/myProject

Before:

>>> myProject.__path__
_NamespacePath(['/path/myProject'])
>>> myProject.__file__
AttributeError: module 'myProject' has no attribute '__file__'

After:

>>> myProject.__path__
['/path/myProject']
>>> myProject.__file__
'/path/myProject/__init__.py'
Answer from Antoine Cotten on Stack Overflow
Top answer
1 of 1
7

I found the solution to my issue, which I believe was a misunderstanding of PEP 420 -- Implicit Namespace Packages.

This page states that, since Python 3.3:

While looking for a module or package named "foo", for each directory in the parent path:

  • If <directory>/foo/__init__.py is found, a regular package is imported and returned.
  • If not, but <directory>/foo is found and is a directory, it is recorded and the scan continues with the next directory in the parent path.

If the scan completes without returning a module or package, and at least one directory was recorded, then a namespace package is created. The new namespace package:

  • Has a __path__ attribute set to an iterable of the path strings that were found and recorded during the scan.
  • Does not have a __file__ attribute.

As defined in the pkg_resources documentation:

A namespace package is a package that only contains other packages and modules, with no direct contents of its own.

My package, as I described it, fell into that category, which was not intended.

After adding an empty __init.py__ below the myProject/ directory I see this happening during the install step:

❯ python setup.py build    
running build
running build_py
creating build/lib
creating build/lib/myProject
copying myProject/__init__.py -> build/lib/myProject
copying myProject/cli.py -> build/lib/myProject

Before:

>>> myProject.__path__
_NamespacePath(['/path/myProject'])
>>> myProject.__file__
AttributeError: module 'myProject' has no attribute '__file__'

After:

>>> myProject.__path__
['/path/myProject']
>>> myProject.__file__
'/path/myProject/__init__.py'
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί help with python packaging, "modulenotfounderror: no module named"
r/learnpython on Reddit: help with python packaging, "ModuleNotFoundError: No module named"
May 15, 2018 -

I have the following project structure:

gammy-cli/
|__env/ (virtualenv directory)
|__gammy/
     |__utils/
          |__log.py
          |____init__.py
          |__ad_config.json
     |___init__.py
     |__cli.py
|__MANIFEST.in
|__README.rst
|__setup.cfg
|__setup.py

These are all located in a virtualenv directory. When i run 'pip install .' from the 'gammy-cli' directory, everything installs fine but when I try to run 'gammy' at the terminal from the 'gammy-cli' directory, I am getting the "ModuleNotFoundError: No module named gammy" error.

Contents of setup.py below:

from setuptools import setup

requirements = [
    'xlrd',
    'ldap3',
    'pyasn1'
]

def readme():
    with open('README.rst') as f:
        return f.read()


setup(
    name='gammy',
    version='0.1',
    description="Basic CLI tool to automate Google group management",
    long_description=readme(),
    author='bob bob,
    author_email='bob@company.com',
    url='https://github.com/bob/gammy',
    entry_points = {'console_scripts': ['gammy=gammy.cli:main',],},
    install_requires=requirements,
    zip_safe=False,
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Natural Language :: English',
        'Operating System :: MacOS X',
        'Programming Language :: Python 3.6',
    ],
)

After i run the pip install . command it seems to create a 'gammy' executable in the gammy-cli/env/bin/ directory. Not sure if that matters. This is my first attempt at trying to create a cli tool so any help would be appreciated. Been looking at this for a few days and googling a lot but haven't found a working solution or a way to diagnose the issue.

Discussions

`ModuleNotFoundError` when running script from `console_scripts` but not when importing via Python console - Stack Overflow
I'm trying to set up a proper Python package for distribution and I'm running into problems with the console_scripts property of my setup.py file. I have the following in my setup: More on stackoverflow.com
🌐 stackoverflow.com
python - ImportError when using console_scripts in setuptools - Stack Overflow
I am trying to build a program ... dnsrep module without using the command python dnsrep. The setup.py script I wrote is given below: from setuptools import setup, find_packages setup( name='dnsrep', version='0.1', description='Program that gives a reputation score to url\'s\n.', entry_points = { 'console_scripts': ... More on stackoverflow.com
🌐 stackoverflow.com
March 20, 2017
setuptools integration using "Scripts in Packages" example - not working
I am following the setuptools example using a python3.7.1 conda virtual env from the documentation here and continue to receive an error: ModuleNotFoundError: No module named 'yourpackage.scrip... More on github.com
🌐 github.com
5
February 20, 2019
Bug: ModuleNotFoundError: No module named 'nibe_mqtt.console_scripts'
hardy@raspi-ds:~/.local/lib/python3.13/site-packages/nibe_mqtt $ nibe-mqtt --help Traceback (most recent call last): File "/home/hardy/.local/bin/nibe-mqtt", line 3, in from nibe_mqtt.console_scripts.nibe_mqtt_service import main ModuleNotFoundError: No module named 'nibe_mqtt.con... More on github.com
🌐 github.com
5
January 5, 2026
Top answer
1 of 16
256

This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run

import os
os.getcwd() 

you'll see this is true.

However, let's say you're using an ipython notebook, run os.getcwd() and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir setting).

The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:

import sys
sys.path.append('my/path/to/module/folder')

import module_of_interest

If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.

The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.

2 of 16
20

Just create an empty python file with the name __init__.py under the folder which showing error, while you running the python project.

🌐
ROS Answers
answers.ros.org β€Ί question β€Ί 322355 β€Ί modulenotfounderror-after-adding-second-python-pkg-to-colcon-workspace
ModuleNotFoundError after adding second Python pkg to colcon workspace - ROS Answers archive
May 2, 2019 - from setuptools import find_packages from setuptools import setup package_name = 'pkg_one' setup( name=package_name, version='0.0.0', packages=find_packages(), install_requires=['setuptools'], zip_safe=True, author='user', author_email="user@todo.todo", maintainer='user', maintainer_email="user@todo.todo", keywords=['ROS', 'ROS2'], classifiers=[ 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', 'Topic :: Software Development', ], description='TODO: Package description.', license='Apache License, Version 2.0', tests_require=['pytest'], entry_points={ 'console_scripts': [ 'pub_one = pkg_one_src.pub_one:main', ], }, ) ========================== REMARKS ======================================== " Changing your python module name to not be src makes sense.
🌐
GitHub
github.com β€Ί pallets β€Ί click β€Ί issues β€Ί 1239
setuptools integration using "Scripts in Packages" example - not working Β· Issue #1239 Β· pallets/click
February 20, 2019 - I am following the setuptools example using a python3.7.1 conda virtual env from the documentation here and continue to receive an error: ModuleNotFoundError: No module named 'yourpackage.scripts' yourpackage/ β”œβ”€β”€ __init__.py β”œβ”€β”€ main.py...
Author: pallets
🌐
Python Packaging
packaging.python.org β€Ί specifications β€Ί entry-points
Entry points specification - Python Packaging User Guide
Within a value, readers must accept and ignore spaces (including multiple consecutive spaces) before or after the colon, between the object reference and the left square bracket, between the extra names and the square brackets and colons delimiting them, and after the right square bracket. The syntax for extras is formally specified in Dependency specifiers. For tools writing the file, it is recommended only to insert a space between the object reference and the left square bracket. ... [console_scripts] foo = foomod:main # One which depends on extras: foobar = foomod:main_bar [bar,baz] # pytest plugins refer to a module, so there is no ':obj' [pytest11] nbval = nbval.plugin
Find elsewhere
🌐
GitHub
github.com β€Ί yozik04 β€Ί nibe-mqtt β€Ί issues β€Ί 27
Bug: ModuleNotFoundError: No module named 'nibe_mqtt.console_scripts' Β· Issue #27 Β· yozik04/nibe-mqtt
January 5, 2026 - Hello, there is a bug in packaging: console_scripts/* is missing. hardy@raspi-ds:~/.local/lib/python3.13/site-packages/nibe_mqtt $ nibe-mqtt --help Traceback (most recent call last): File "/home/hardy/.local/bin/nibe-mqtt", line 3, in
Author: yozik04
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 73269814 β€Ί console-scripts-outside-the-main-module-arent-packaged-along-modulenotfounderr
setuptools - Console scripts outside the main module aren't packaged along (ModuleNotFoundError) - Stack Overflow
August 7, 2022 - `ModuleNotFoundError` when running script from `console_scripts` but not when importing via Python console ... "No module named x.__main__; 'x' is a package and cannot be directly executed" when using entry_points / console_scripts
🌐
Containersolutions
containersolutions.github.io β€Ί runbooks β€Ί posts β€Ί python β€Ί module-not-found
Module Not Found – Runbooks
Traceback (most recent call last): File "afile.py", line 1, in <module> import doesnotexist ImportError: No module named doesnotexist Β· When you run a Python application, script, or interactive terminal, you see an error that looks like the above.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί going crazy with "no module named" error - formerly working
r/learnpython on Reddit: Going crazy with "no module named" error - formerly working
December 17, 2019 -

I feel like I've tried the usual stuff, but I remain stumped here. This code used to work in the terminal and continues to work in pycharm. I get this error in the terminal :

$ python A_inventory_fetch.py 
Traceback (most recent call last):
  File "A_inventory_fetch.py", line 58, in <module>
    main()
  File "A_inventory_fetch.py", line 14, in main
    import Scripts.connection_methods.credentials as credentials
ModuleNotFoundError: No module named 'Scripts'

Here's the folder structure:

Scripts

    β”œβ”€β”€ Sync_Inventory
    β”‚   β”œβ”€β”€ A_inventory_fetch.py
    β”‚   β”œβ”€β”€ B_oh_by_variant.py
    β”‚   β”œβ”€β”€ C_create_sync_data.py
    β”‚   β”œβ”€β”€ D_update_site.py
    β”‚   β”œβ”€β”€ E_runsync.py
    β”‚   └── __init__.py
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ connection_methods
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ credentials.py
    β”‚   β”œβ”€β”€ client_secrets.json
    β”‚   β”œβ”€β”€ api_conn.py
    β”‚   
    └── requirements.txt

What did I screw up here?

Edit to add: the end goal is to restore my ability to run E_runsync.py from crontab on a remote server. Somewhere along the road with edits, I screwed something up and now get the "No Module named.. " error

🌐
RoseIndia
roseindia.net β€Ί answers β€Ί viewqa β€Ί pythonquestions β€Ί 64609-ModuleNotFoundError-No-module-named-console.html
ModuleNotFoundError: No module named 'console'
September 8, 2020 - named 'console_script' How to remove the ModuleNotFoundError: No module named... the installation of console_script python library, ModuleNotFoundError: No module named
🌐
Readthedocs
python-packaging.readthedocs.io β€Ί en β€Ί latest β€Ί command-line-scripts.html
Command Line Scripts β€” Python Packaging Tutorial
This allows Python functions (not scripts!) to be directly registered as command-line accessible tools. In this case, we’ll add a new file and function to support the command line tool: funniest/ funniest/ __init__.py command_line.py ... setup.py ... The command_line.py submodule exists only to service the command line tool (which is a convenient organization method): ... setup( ... entry_points = { 'console_scripts': ['funniest-joke=funniest.command_line:main'], } ...
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 49418808 β€Ί python-no-module-named-error-in-the-python-console
import - Python "no module named" error in the python console - Stack Overflow
March 22, 2018 - I have installed numpy and requests though pip install. I open Pycharm and type in the console: import requests or import numpy I get: Traceback (most recent call last): File " ",
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 47800969 β€Ί custom-modules-are-not-found-when-calling-python-script-in-console-vs-pycharm
Custom modules are not found when calling python script in console VS pycharm - Stack Overflow
December 14, 2017 - Python instead has no clue; so unless the module is in the PYTHONPATH variable, it will raise an error. Thanks for pointing me to the right place! 2017-12-13T22:14:40.557Z+00:00 ... On PyCharm, next to the green "RUN" arrow press the box and then press edit configurations (see image) There you'll have Working Directory - that path is where PyCharm is running that script from (without errors).
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί 'module not found' when running from command line, but fine in ide - only for modules outside of standard library
r/learnpython on Reddit: 'Module not found' when running from command line, but fine in IDE - only for modules outside of standard library
November 16, 2018 -

I'm using Python 3.6.5 on Windows 10.

When I try to run a script from the command line which imports certain modules, like praw, I get ModuleNotFoundError: No module named 'praw'. However, doing pip install praw or python -m pip install praw gives me a list of Requirement already satisfieds. And when I run the script from IDLE, it goes without a hitch.

This doesn't happen for all modules. I tested a bunch, and the following modules throw an error: praw, gspread, prawcore, openpyxl. These are fine: datetime, sqlite3, time, logging, sys, re, glob, collections. So it looks like the problem is limited to modules outside the standard library.

The problem started after installing PyCharm. A year ago, the same thing happened - I installed PyCharm and couldn't run scripts from the command line anymore. I didn't troubleshoot, just uninstalled PyCharm and that resolved everything. This week, I thought that the program was worth another try and installed it again, but the command line issue came up again and I couldn't fix it. Unfortunately, last year's strategy didn't work: I've removed PyCharm from my laptop, and I rebooted - but the problem persists.

I've googled myself silly, and I can't figure out what's going on. Hope someone can help!