1

I've been building a simple CLI.

My main.py looks like this:

import prompt <- This is my file
import utils <- This too.

# blah blah blah

When I just ran python3 main.py, it works just fine. But after running python3 setup.py install and running myCLI somecommand on my terminal, I got this:

0.0.1-py3.9.egg/testCVRCLI/main.py", line 1, in <module>
ModuleNotFoundError: No module named 'prompt'

I checked whether it works by running python3 main.py, and it worked fine.

EDIT

# main.py

import prompt
import utils
import click

@click.group()
def main():
    pass

@main.command()
def setupdb():
    # some code

@main.command()
def create_data():
   # some code

if __name__ == "__main__":
    if platform.system() != "Darwin":
        utils.print("This is currently only for Mac users.")
    else :
        main()

My directory structure looks like this:

testCVRCLI
      |- setup.py
      |- CVRCLI_folder
           |- __init__.py
           |- main.py
           |- prompt.py
           |- utils.py

My setup.py

from setuptools import setup, find_packages

# Setting up
setup(
        name="myCLI", 
        version=VERSION,
        author="My Name",
        author_email="My Email",
        description=DESCRIPTION,
        long_description=LONG_DESCRIPTION,
        long_description_content_type="text/markdown",
        packages=find_packages(),
        install_requires=[
            "click",
            "mysql-connector-python",
            "protobuf",
            "six",
        ], 
        license="MIT",
        classifiers= [
            "Environment :: Console",
            "Development Status :: 2 - Pre-Alpha",
        ],
        entry_points = """
            [console_scripts]
            myCLI=CVRCLI_folder.main:main
        """
)
leaves
  • 111
  • 1
  • 12
  • What is `prompt`? Is it a module in your project (i.e. `prompt.py`) or a third party package? What does your `setup.py` look like? – Selcuk Sep 06 '21 at 03:41
  • It is my file i created. –  Sep 06 '21 at 03:41
  • 1
    Can you find it under `0.0.1-py3.9.egg/testCVRCLI/` directory? – Selcuk Sep 06 '21 at 03:42
  • 7
    If possible then share some more details of `main.py` – Sabil Sep 06 '21 at 03:43
  • `0.0.1-py3.9.egg` is a file that looks like this: `6413 aacf caec 6deb 5a4f baf6 a949 b6f0` –  Sep 06 '21 at 03:45
  • I don't understand why, but there's not directory under `0.0.1-py3.9.egg` –  Sep 06 '21 at 03:45
  • Did you remember to package the files in `setup.py`? It seems you're messing up the packaging. – Alex Huszagh Sep 06 '21 at 03:49
  • @AlexanderHuszagh What should I do in `setup.py`? –  Sep 06 '21 at 03:51
  • can you share a detailed directory structure with us? – Duffy Sep 06 '21 at 03:52
  • @AgentNoby Normally you use `find_packages`, which handles everything, but there's more extensive configurations if you need [them](https://setuptools.readthedocs.io/en/latest/setuptools.html). You can manually specify paths as well. Maybe share your `setup.py`, which is more important than anything else here. – Alex Huszagh Sep 06 '21 at 03:55
  • 2
    I beleive that because main.py is in your package, you should refer to the other modules it imports from the same package relative to the package root, i.e. `import .prompt` rather than `import prompt` – David Waterworth Sep 06 '21 at 03:59
  • No. I can't do that. I got error doing it on VSCode –  Sep 06 '21 at 04:00
  • Either than or move main.py outside the package – David Waterworth Sep 06 '21 at 04:00
  • I still get the same error. –  Sep 06 '21 at 04:03
  • I think it should be `import CVRCLI_folder.prompt` then, if you're running using F5 from vscode it'll set the working directory to be the root folder. – David Waterworth Sep 06 '21 at 04:05
  • See this version https://github.com/allenai/allennlp/blob/main/allennlp/__main__.py - in the `run()` command (similar to your `main()` ) they import `allennlp.commands` not `commands` or `.commands` – David Waterworth Sep 06 '21 at 04:09
  • I did it but i still get the same error . –  Sep 06 '21 at 04:09
  • Does `import myCLI.prompt` work? – David Waterworth Sep 06 '21 at 04:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236795/discussion-between-agent-noby-and-david-waterworth). –  Sep 06 '21 at 04:13

1 Answers1

0

I solved this like this:

try:
    import utils
    import prompt
except ModuleNotFoundError:
    from . import utils
    from . import prompt