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
  qpython package error Creepy 5 1,038 Today, 10:35 AM
Last Post: masonsbore
  importing variables from module 8376459 1 274 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 851 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  is import cointegration_analysis a recognized module mitcht33 1 425 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,394 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 716 Aug-06-2023, 01:09 AM
Last Post: aupres
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,672 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  pyarrow error when importing pandas sravva 1 933 Jun-06-2023, 05:09 PM
Last Post: snippsat
  import module error tantony 5 3,443 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs

Forum Jump:

User Panel Messages

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