简体   繁体   English

Python 从自己的文件导入函数

[英]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 .我创建了一个使用一些函数的脚本,但现在我已将这些函数移动到名为的文件夹中: include The file is called: mylib.py , but when I use the following code:该文件名为: mylib.py ,但是当我使用以下代码时:

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

import mylib

It gives an error: No module named 'mylib' .它给出了一个错误: No module named 'mylib' The main code is in the windows directory: Desktop/Python/ and the include file in: Desktop/Python/include/ .主要代码在 windows 目录: Desktop/Python/和包含文件: Desktop/Python/include/

What am I doing wrong?我究竟做错了什么?

add an empty file __init__.py to the include folder to make it a package.将一个空文件__init__.py添加到include文件夹以使其成为一个包。

then import from it with:然后从它导入:

from include import mylib

Replace the second line with this one:用这一行替换第二行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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