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
#1
I have a folder 'home', containing another folder (package) that contains couple python files (modules).
--- home
------ pkg
--------- mod1.py
--------- mod2.py

Inside mod1.py, I am importing mod2.py:

mod1.py:
import mod2
# use symbols from mod2 ...
Now, suppose I create, inside 'pkg', a 'client' code that wants to import (and use) symbols from mod1.py
--- home
------ pkg
--------- mod1.py
--------- mod2.py
--------- client.py

client.py:
#!/usr/local/bin/python3
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
--------- mod1.py
--------- mod2.py
------ client.py

client.py:
#!/usr/local/bin/python3
import pkg.mod1
# use symbols from mod1 ...
I get the following:
Output:
Traceback (most recent call last): File "./client.py", line 2, in <module> import pkg.mod1 File "/home/pkg/mod1.py", line 1, in <module> import mod2 ModuleNotFoundError: No module named 'mod2'
Now, I understand that the current working directory for client.py 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 client.py 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?
Reply
#2
If you want to run your program from /home/pkg then you can actually make a call to change your directory path to /home/pkg using os.chdir() in client.py
Reply
#3
(Mar-27-2020, 06:42 PM)Malt Wrote: If you want to run your program from /home/pkg then you can actually make a call to change your directory path to /home/pkg using os.chdir() in client.py

I don't want to force the client into doing that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  import module with syntax error Skaperen 7 709 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Importing module in jupyter Noteboook ajitnayak1987 0 501 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  Import Error Christian 3 958 Apr-17-2021, 11:27 PM
Last Post: bowlofred
  ERROR: importing desired module mbgamer28 0 487 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  countryinfo package error MarcusAurelius 4 758 Mar-20-2021, 05:42 PM
Last Post: deanhystad
  Can't import package hubenhau 1 1,724 Jan-15-2021, 06:23 PM
Last Post: buran
  'urllib3' Module not found when import 'requests' spanz 5 2,771 Jan-06-2021, 05:57 PM
Last Post: snippsat
  Folium import error kashif_flavio 2 923 Dec-07-2020, 08:58 AM
Last Post: bowlofred
Bug Error while importing numpy Erfan 3 1,489 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  Import error Beingcode 0 976 Nov-20-2020, 02:57 AM
Last Post: Beingcode

Forum Jump:

User Panel Messages

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