How to use import statement for custom modules in Python

I am very new to python programming and writing a simple helloworld program using python 3.3 for windows environmentoment. The helloworld program is saved as hello.py. So how can I use it in another module. I tried sys.path.append and gave the path to my save file but it didn't work. Can someone tell me that I need to set the environment variable in windows xp

Thanks.

+7
source share
3 answers

Use this method:

import sys 

then

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

Where X is the directory from which you want to import.

After that, you just need to import your custom module:

 import X 

That's all.

+7
source share

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

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

Imports a module, for example. X = hello .

+3
source share

If you are trying to create your hello.py module, you need to create a file called __init.py__ in the hello.py folder.

Take a look at the Python documentation here .

+1
source share

All Articles