How to use import statement for custom modules in Python

后端 未结 3 1266
死守一世寂寞
死守一世寂寞 2021-02-04 17:18

I am very new to python programming and writing simple helloworld program by using python 3.3 on windows environoment.The helloworld program is saved as hello.py. So how can i

相关标签:
3条回答
  • 2021-02-04 17:57

    If you're trying to make your hello.py a module you need to create a file named __init.py__ in the hello.py folder.

    Take a look at the Python documentation here.

    0 讨论(0)
  • 2021-02-04 18:16

    There is a directory DIR containing our module X: /DIR/X

    import sys    
    sys.path.insert(0,"DIR")
    import X
    

    This imports the module e.g. X =hello.

    0 讨论(0)
  • 2021-02-04 18:19

    Use this way:

    import sys
    

    then:

    sys.path.insert(0,"X")
    

    Where X is the directory you want to import from.

    After that you just need to import your custom module:

    import X
    

    Thats all.

    0 讨论(0)
提交回复
热议问题