A. Vreeswijk
A. Vreeswijk

Reputation: 954

Python Import functions from own file

I have a problem. I created a script that uses a few functions, but now I have moved those functions in a folder named: include. The file is called: mylib.py, but when I use the following code:

import sys
sys.path.insert(0, 'include/')

import mylib

It gives an error: No module named 'mylib'. The main code is in the windows directory: Desktop/Python/ and the include file in: Desktop/Python/include/.

What am I doing wrong?

Upvotes: 1

Views: 138

Answers (2)

Pavlos
Pavlos

Reputation: 934

Replace the second line with this one:

sys.path.insert(0, '/Desktop/Python/include')

Upvotes: 0

dzang
dzang

Reputation: 2260

add an empty file __init__.py to the include folder to make it a package.

then import from it with:

from include import mylib

Upvotes: 3

Related Questions