#๐Ÿ”’ Python relative import error: Module Not Found

42 messages ยท Page 1 of 1 (latest)

fallow badger
#

I have the following structure:

Common 
| - - - -  __init__.py
| - - - - ConfigHelper.py
scripts
| - - - - main.py (from ConfigHelper import Config) (It does not creates error)
| - - - - utils/
              | - - - - shared_db_connection_repository.py 

I am importing a class from ConfigHelper file in various different files located in the same utils and scripts directory.

I am importing using the following code from Common.ConfigHelper import Config, which is correctly get accessed in the main file.

But it creates error when accessed with the same method inside files present under utils directory.

I have added Common in the extraPaths inside settings.json so VS code does able to locate the reference, but when I run the script using python -u "absolute-path-to-file" it creates error:

shared_db_connection_repository.py", line 6, in <module>
    from Common.ConfigHelper import Config
ModuleNotFoundError: No module named 'Common'
wise moonBOT
#

@fallow badger

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.

hushed elbow
#

the import indicates that ConfigHelper is a top level package which it is not here

#

I think you wanted from .ConfigHelper import Config

#

but you really ought to name your packages and modules in lowercase to distinguish them from class names

fallow badger
#

let me update the message, I accidently posted it before writing it completely.

hushed elbow
#

so where is the relative import you wanted help with?

fallow badger
hushed elbow
#

so what is your question?

fallow badger
#

can you look now ?

hushed elbow
#

it sounds like the package in question isn't actually installed

#

and thus isn't on sys.path

fallow badger
#

well, I know the sys.path solution, but I don't want to add the same sys.path.append() to all of my files.

So looking for a simple solution which is neat and clean.

hushed elbow
#

I kind of implied what the solution is

#

install the package

fallow badger
#

it is not the package, it is the folder which I made named "Common"

hushed elbow
#

a package is a folder with an __init__.py module in it

fallow badger
#

oh ohkay, that way.

#

yes it is module

#

but why do I need to install ?

hushed elbow
#

so that it's present on sys.path regardless of what the current directory is

#

alternatively you can set PYTHONPATH to point to the directory containing Common

fallow badger
#

ohkay, how can i do that ?

hushed elbow
#

if you want to go the packaging route, you'll need a minimal pyproject.toml in the project root (the directory containing the top level package, Common)

#

then you'd do an editable install with pip install -e .

fallow badger
#

so is it the only solution ?

hushed elbow
#

are you reading my messages? I gave you two alternatives

fallow badger
#

By the way,
Thank you for Keep answering my queries ๐Ÿ™‚

fallow badger
hushed elbow
#

ok, but why then are you asking if this is the only solution when I already gave you two?

#

the other one being to set PYTHONPATH to point to the project directory

fallow badger
#

the thing is, this project will be run as azure function app, so I need to find the solution which is compatible with that envrionment

hushed elbow
#

the packaging solution is more robust as it also allows you to define run-time (and build-time) dependencies

fallow badger
hushed elbow
#

setting PYTHONPATH is simpler and will probably work with azure functions

fallow badger
#

Thank @hushed elbow I'm going with pyproject.toml solution, and discussed the same with my team lead as well

#

Will ping you if I got some queries then.

#

Once again thank you for your time ๐Ÿ™‚ ๐Ÿ™

wise moonBOT
#
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.