Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post 3 months ago by Alexei‭.

17 / 255
  • `ModuleNotFoundError` means that in running your program, Python attempted to import some module `xyz` but it was not present on your system.
  • It could be that you forgot to install the module (forgot to `pip install -r requirements.txt`), misspelled the module name, or tried to import a module that doesn't exist. But that would be somewhat obvious so let's assume it's not that, and the module name is correct. However, do note that some packages on Pypa for example have surprising import names - sometimes you `pip install xyz` but `import pyxyz` etc - this should normally be covered in the package's documentation.
  • The first useful thing is to find a way to test the import easily. If you got `ModuleNotFoundError: No module named 'xyz'`, you can reproduce it by running `python -c 'import xyz` in your shell: If the import succeeds, it will print nothing and exit with code 0. If the import fails, you will see the `ModuleNotFoundError` again.
  • Often, package imports fail due to an environment mismatch. The package is actually installed, but in another Python venv, and not in the current one. It's of course hard to find putative "other* venvs on your computer, but you can look in obvious places like a `venv/` dir in the project directory. `pip list` will show the packages installed in the *current* environment, so that can help identify them. You can of course just install them again in the current environment as an alternative to looking for the other one.
  • The environment stuff often comes up due to using "helper" programs, like conda or pyenv. IDEs with integrated Python consoles, like VS Code's Python tab, can also start it with a different environment than your terminal. It's useful to be mindful of which helper programs you are using and how they handle environments, and avoid helpers that are hard to understand.
  • As a brute force solution, you can try searching for the package or module `xyz` on your computer: `rg xyz /` (`rg` is https://github.com/BurntSushi/ripgrep). Because of Python's import system and conventions, if there is an importable `xyz`, there is probably a file `xyz.py` somewhere. You can analyze its path to figure out what environment it is.
  • `ModuleNotFoundError` means that in running your program, Python attempted to import some module `xyz` but it was not present on your system.
  • It could be that you forgot to install the module (forgot to `pip install -r requirements.txt`), misspelled the module name, or tried to import a module that doesn't exist. But that would be somewhat obvious, so let's assume it's not that, and the module name is correct. However, do note that some packages on PyPI for example have surprising import names - sometimes you `pip install xyz` but `import pyxyz` etc - this should normally be covered in the package's documentation.
  • The first useful thing is to find a way to test the import easily. If you got `ModuleNotFoundError: No module named 'xyz'`, you can reproduce it by running `python -c 'import xyz'` in your shell: If the import succeeds, it will print nothing and exit with code 0. If the import fails, you will see the `ModuleNotFoundError` again.
  • Often, package imports fail due to an environment mismatch. The package is actually installed, but in another Python venv, and not in the current one. It's of course hard to find putative "other" venvs on your computer, but you can look in obvious places like a `venv/` dir in the project directory. `pip list` will show the packages installed in the *current* environment, so that can help identify them. You can of course just install them again in the current environment as an alternative to looking for the other one.
  • The environment stuff often comes up due to using "helper" programs, like conda or pyenv. IDEs with integrated Python consoles, like VS Code's Python tab, can also start it with a different environment than your terminal. It's useful to be mindful of which helper programs you are using and how they handle environments, and avoid helpers that are hard to understand.
  • As a brute force solution, you can try searching for the package or module `xyz` on your computer: `rg xyz /` (`rg` is https://github.com/BurntSushi/ripgrep). Because of Python's import system and conventions, if there is an importable `xyz`, there is probably a file `xyz.py` somewhere. You can analyze its path to figure out what environment it is.

Suggested 3 months ago by Karl Knechtel‭