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
Question Setuptools CLI program ModuleNotFoundError after splitting up module into package Postbote 1 78 Nov-25-2021, 06:35 PM
Last Post: Postbote
  Import a module for use in type hint? Milosz 0 132 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 375 Oct-27-2021, 03:32 AM
Last Post: Tek
  Unable to import sklearn after installing any package ilango 0 197 Oct-25-2021, 07:03 AM
Last Post: ilango
  import module with syntax error Skaperen 7 957 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Importing module in jupyter Noteboook ajitnayak1987 0 627 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  Import Error Christian 3 1,127 Apr-17-2021, 11:27 PM
Last Post: bowlofred
  ERROR: importing desired module mbgamer28 0 561 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  countryinfo package error MarcusAurelius 4 918 Mar-20-2021, 05:42 PM
Last Post: deanhystad
  Can't import package hubenhau 1 2,003 Jan-15-2021, 06:23 PM
Last Post: buran

Forum Jump:

User Panel Messages

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