#๐Ÿ”’ ModuleNotFoundError when importing from local package after pip install

18 messages ยท Page 1 of 1 (latest)

wraith venture
#

I'm working on a new project called ContentOrganizer, and I want to reuse some utilities from an older project of mine called SortPhotos. In SortPhotos, Iโ€™ve added a setup.py and an init.py, and it includes a module called file_utils.py that I want to import.

I was able to install the package into ContentOrganizer using pip install /path/to/SortPhotos, but when I try to import it like this:

from SortPhoto import file_utils

I get this error:

ModuleNotFoundError: No module named 'SortPhoto'

blazing masonBOT
#

@wraith venture

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

proud ridge
#

also you should prefer pyproject.toml instead of setup.py

wraith venture
#

ls
build data_processing.py dist file_utils.py __init__.py main.py outtext __pycache__ README setup.py SortPhoto.egg-info tests venv

proud ridge
#

project structure should be similar to this. ```
projectname/
.git/
.venv/
projectname/
init.py
tests/
test_mything.py
README.md
setup.py

wraith venture
# proud ridge What's the `tree` of your sortphoto project?
โ”œโ”€โ”€ data_processing.py
โ”œโ”€โ”€ dist
โ”œโ”€โ”€ file_utils.py
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ main.py
โ”œโ”€โ”€ outtext
โ”œโ”€โ”€ __pycache__
โ”œโ”€โ”€ README
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ SortPhoto.egg-info
โ”œโ”€โ”€ tests
โ””โ”€โ”€ venv```
proud ridge
#

your .py files should be in a package folder

wraith venture
#

like this .
โ”œโ”€โ”€ README
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ SortPhotos
โ”œโ”€โ”€ data_processing.py
โ”œโ”€โ”€ file_utils.py
โ”œโ”€โ”€ init.py
โ””โ”€โ”€ main.py

proud ridge
#

yes.

#

If you're doing python SortPhotos/main.py, it's probably a mistake

wraith venture
#

what should it be instead?

proud ridge
#

either a console script defined in setup.py or python -m SortPhotos.main

#

if you name it __main__.py you can run it via python -m SortPhotos

wraith venture
#

sorry i need to go for about a hour and will be right back after

neat geyser
blazing masonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.