简体   繁体   中英

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?

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

then import from it with:

from include import mylib

Replace the second line with this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM