简体   繁体   English

从另一个目录导入python模块

[英]Importing python modules from another directory

I am trying to import files (python files) from another directory but it is not working as I have tried. 我正在尝试从另一个目录导入文件(python文件),但是它无法正常运行。

/pythonproject
.main.py
.__init__.py
 ->folder1
   ->.file1.py
   ->.file2.py
   ->.__init__.py
 ->folder2
   ->.functions.py
   ->.globals.py
   ->.__init__.py

I am trying to import functions.py inside of my file2.py. 我正在尝试在我的file2.py中导入functions.py。

I have tried 我努力了

from functions import *
import functions

#file2.py
sys.path.insert(0, '/pythonproject')
import functions

我认为在这种情况下,您的朋友是sys.path.append

As discovered the problem is that you are importing the file, but when you went to use a function in the file you couldn't 发现的问题是您正在导入文件,但是当您去使用文件中的函数时却无法

One solution is: 一种解决方案是:

import functions

functions.NameOfFunctionHere()

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

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