• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Python saying file does not exist when importing but clearly does

Resolved: Python saying file does not exist when importing but clearly does

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am writing a simple pygame name generator for a little project and am trying to import a function I made but it says it does not exist. You can see: here that my from and imports are correct. The script that runs is fantasy name generator yet this error (in the screenshot) is produced. Any help would be appreciated, thanks.
My folder structure:
This image is of my folder structure

Answer:

generate_name.py is in a deeply nested package alongside the other module. Python 3 uses absolute imports by default, relative imports must be performed explicitly.
The relative import you’d want here is (note leading . indicating a relative name):
from .prefix_code_gen import prefix_code_gen
The absolute import path would match the same one you used for generate_name if you wanted to use it:
from functional.generators.prefix_code_gen import prefix_code_gen
I’m inclined to favor the relative import, since it will continue to work even if a refactoring changes the absolute path, as long as the two modules are still installed under the same package directory.

If you have better answer, please add a comment about this, thank you!

pygame python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to scroll bottom of div at launch?

02/04/2023

Resolved: how to get and read an xml file in a zip file using xml.etree

02/04/2023

Resolved: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed. cors error not resolving

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.