Python Forum
Importing module from a package results in import error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing module from a package results in import error
I have a folder 'home', containing another folder (package) that contains couple python files (modules).
--- home
------ pkg

Inside, I am importing
import mod2
# use symbols from mod2 ...
Now, suppose I create, inside 'pkg', a 'client' code that wants to import (and use) symbols from
--- home
------ pkg
import mod1
# use symbols from mod1 ...
That all works fine, but watch what happens when the client is importing mod1 from outside 'pkg':
--- home
------ pkg
import pkg.mod1
# use symbols from mod1 ...
I get the following:
Traceback (most recent call last): File "./", line 2, in <module> import pkg.mod1 File "/home/pkg/", line 1, in <module> import mod2 ModuleNotFoundError: No module named 'mod2'
Now, I understand that the current working directory for has changed from '/home/pkg/' to '/home/' and that python is searching for 'mod2' at '/home/' instead of at '/home/pkg/', but how can I overcome that?
I want the to be able to run from wherever, and for 'mod1' to be able to import 'mod2' regardless of what's the current working directory is.
Is it possible?

Messages In This Thread
Importing module from a package results in import error - by goghvv - Mar-27-2020, 06:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cryptic Error with import statement Led_Zeppelin 2 178 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 226 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  "ModuleNotFoundError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package" chipx 3 371 Dec-09-2021, 07:05 AM
Last Post: chipx
Question Setuptools CLI program ModuleNotFoundError after splitting up module into package Postbote 1 346 Nov-25-2021, 06:35 PM
Last Post: Postbote
  Import a module for use in type hint? Milosz 0 232 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 601 Oct-27-2021, 03:32 AM
Last Post: Tek
  Unable to import sklearn after installing any package ilango 0 305 Oct-25-2021, 07:03 AM
Last Post: ilango
  import module with syntax error Skaperen 7 1,256 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Importing module in jupyter Noteboook ajitnayak1987 0 760 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  Import Error Christian 3 1,309 Apr-17-2021, 11:27 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020