Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module import error
#1
When I'm in the directory 'sub_pckg2' and run python mod3.py in the terminal, I get a modulenotfounderror but this error does not occur when I run the program from my IDE editor which in this case is Pycharm.

#pkg/sub_pckg2/mod3.py
from pkg.sub_pkg1.mod1 import foo

foo()

#pkg/sub_pckg1/mod1.py
def foo():
    print('[mod1] foo()')
If I change the code as follows, adding the two lines in bold the error goes away and I do not understand why.
#pkg/sub_pckg2/mod3.py
import sys
[b]base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")[/b]
from pkg.sub_pkg1.mod1 import foo

foo()

#pkg/sub_pckg1/mod1.py
def foo():
    print('[mod1] foo()')
Also, suppose I had 10 files in the folder sub_pckg2 and they all used mod1.foo I would then have to put
base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")
At the top of each file. Further, suppose I deleted mod1.py, I would then have to go back to all 10 files and make the proper changes. My way around this is to add a file:

#add_path.py
import sys
base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")
And then write simply at the top of each file:

import add_path

But is there a better way of doing things?

I also don't understand why I get an import error when I do the following:

##sub_pckg2.mod3.py
from . import mod4

qux()


##sub_pckg3.mod4.py
def qux():
    print ("hey")

testing

Actually, I see in the Python Cookbook that it says:

Quote:relative imports only work for modules that are located inside a proper package
But I don't know exactly what a proper package is unless it's one where each subfolder has the file __init__.py in it.
Reply


Messages In This Thread
module import error - by bobsmith76 - Sep-16-2018, 11:11 PM
RE: module import error - by Larz60+ - Sep-17-2018, 12:00 AM
RE: module import error - by bobsmith76 - Sep-17-2018, 12:54 AM
RE: module import error - by Mekire - Sep-17-2018, 04:36 AM
RE: module import error - by bobsmith76 - Sep-18-2018, 03:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 385 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,260 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,169 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  import module error tantony 5 3,362 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,036 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Cryptic Error with import statement Led_Zeppelin 2 2,479 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 10,490 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  Import a module for use in type hint? Milosz 0 1,455 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,741 Oct-27-2021, 03:32 AM
Last Post: Tek

Forum Jump:

User Panel Messages

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