user2696156
user2696156

Reputation: 337

How to use import statement for custom modules in Python

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 use it in another module. I tried sys.path.append and give path of my save file but its not working. Can somebody tell me do i have to set Environment variable in windows xp

Thanks.

Upvotes: 7

Views: 15052

Answers (3)

user1938667
user1938667

Reputation:

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.

Upvotes: 1

Mateusz
Mateusz

Reputation: 49

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.

Upvotes: 3

Odai Al-Ghamdi
Odai Al-Ghamdi

Reputation: 302

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.

Upvotes: 7

Related Questions