Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3

后端 未结 7 1325
灰色年华
灰色年华 2020-12-04 05:23

I have a directory structure similar to the following

meta_project
    project1
        __init__.py
        lib
            module.py
            __init__.py         


        
7条回答
  •  囚心锁ツ
    2020-12-04 06:12

    Here's my 2 cents:

    import sys

    map the path where the module file is located. In my case it was the desktop

    sys.path.append('/Users/John/Desktop')

    Either import the whole mapping module BUT then you have to use the .notation to map the classes like mapping.Shipping()

    import mapping #mapping.py is the name of my module file

    shipit = mapping.Shipment() #Shipment is the name of the class I need to use in the mapping module

    Or import the specific class from the mapping module

    from mapping import Mapping

    shipit = Shipment() #Now you don't have to use the .notation

提交回复
热议问题