Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
How to Use Custom Modules?
  • Xin
    Posts: 3,251
    Say i have module1.py in the same directory how would i use it?
    Xin

  • import module1.py

    Easy as pi ;P
    Or you could

    from module1.py import *
  • Xin
    Posts: 3,251
    Ahh thats why, i was doing

    import module1
    from module1 import ...

    Where it should have been module1.py

    Thanks guys
    Xin
  • said:


    That is very useful but thats if it's in a different directory ;)

    Also you could just copy it to where Python scripts usually are such as os and sys. Then you can just import it.
  • Sh3llc0d3
    Posts: 1,910
    Lol it was only a quick google :P
  • sangf
    Posts: 203
    i think you were right the first time you just need to know that it doesn't automatically import everything into the current namespace.


    # module1.py
    def print_stuff(): print('stuff\n')



    # main.py - test use of module: module1
    import module1
    module1.print_stuff() # call print_stuff from the module1 namespace


    alternatively:

    # main.py - test use of module: module1
    from module1 import print_stuff # import print_stuff to current namespace
    # from module1 import * # import everything to current namespace
    print_stuff()