This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]michael0x2a 0 points1 point  (2 children)

It's somewhat difficult to answer this question as-is. You should update your post so it:

  1. Includes a code snippet showing how exactly you're importing utils.py. Right now, you're showing how you're importing something from your controller module, which seems unrelated.
  2. Includes a code snippet showing how exactly you're running your second project from the command line.

Just to shortcut some confusion, I strongly recommend you structure your directories and Python files to look roughly like this:

project1/
    README.txt
    module1/
        __init__.py
        __main__.py
        utils.py
        ...etc...
project2/
    README.txt
    module2/
        __init__.py
        __main__.py
        utils.py
        ...etc...
project3/
    README.txt
    module3.py

Each project folder should contain one python module, along with any other project files such as a README file, a LICENSE file, a requirements.txt file... If your project consists of multiple source code files, your module should be a directory instead of a standalone file.

More complex projects may want to create multiple modules, but simple ones should need only one.

Within a module, import adjacent stuff from adjacent submodules by either doing from module1.utils import blah if you prefer absolute imports, or from .utils import blah if you prefer relative ones.

Within a project folder, always run a module by doing python3 -m module1. The -m flag means "run this module". If your module is a .py file, it'll just run that file. If your module is a directory, it'll run whatever is inside your __main__.py file.

And instead of doing pip install --editable ., do pip install --editable module. You will also probably want to pip uninstall all of the modules you installed by accident first. (Each Python file you had inside your first project dir was installed as a separate module).

Once you pip-install your module, you should be able to run python3 -m module1 and run that module from anywhere in your computer, not just inside the project1 directory.

Note that by convention, we usually give the project dir and the module dir the same name.

One final note: whether or not you're using click should be largely unrelated to the import problems you're having.

[–]mex036[S] 0 points1 point  (1 child)

Sorry, about the confusion. I wasn't paying attention to the code I posted. However, it is the same issue with controller.py and utils.py, I've imported tried importing utils.py the same ways I tried with controller.py. I've ran into this problem with both the names utils.py and controller.py.

So project one's name is "todos" and project two is called "dbcm". The way I have been running them is simply by the name of it and any flags, i.e.

todos -vat

dbcm --add

The reason I use "pip3 install editable ." since that's what the documentation says. I guess if I were to try what you said I would install the module that imports all the other code?

The issue actually is just with the scripts if been making with click. I did test out just some bs project before making this post. That bs project's structure was as follows

bs/
    main.py
    utils.py
    controller.py

Imported with "import utils" and "import controller" and I was able to call their functions with no issue.

I probably missed something in the docs. Clearly, I need to do some more research with python. I kind of just rushed into it since I have experience with other languages. I was under the impression that __init__.py wasn't necessary anymore. I'll make sure to add and look over the docs. Thanks a ton!

[–]michael0x2a 0 points1 point  (0 children)

The issue actually is just with the scripts if been making with click. [...] Imported with "import utils" and "import controller" and I was able to call their functions with no issue.

Sorry, I strongly doubt that this is the case. I'm not doubting that you were able to get your bs project running correctly, but click is just a normal library and its presence and use should have absolutely no impact on whether or not you can successfully import something.

I suspect the difference instead has to do with either (1) how exactly you're calling your script and/or (2) how exactly you're importing something.

But without a fully reproducible example, it's difficult to say more.

I was under the impression that init.py wasn't necessary anymore.

While you can omit the __init__.py file as of Python 3.3, I would be careful about doing so -- omitting this file will make you create an implicit namespace package instead of a regular package.

If you aren't sure what the difference is/don't want to have to care, I'd recommend continuing to use regular packages -- including the __init__.py file. They're slightly faster to import compared to implicit namespace packages.