[英]Import from python file located in sub folder
I'm using vs code.我正在使用 vs 代码。 I'm running file1.py, it imports a function from file2.py.
我正在运行 file1.py,它从 file2.py 导入一个 function。
The file structure is as follows:文件结构如下:
feeds
├── bulk_load
│ ├── __init__.py (empty)
│ └── file2.py
├── __init__.py (empty)
└── file1.py
in file1.py
the following works:在
file1.py
中,以下工作:
from bulk_load.file2 import func123
but the following doesn't:但以下内容不是:
sys.path.append("bulk_load")
from file2 import func123
Error is错误是
ModuleNotFoundError
No module named file2
I dont really understand why.我真的不明白为什么。
I have just recreated with the file structure我刚刚用文件结构重新创建
feeds
file1.py
bulk_load
file2.py
in file1.py:在 file1.py 中:
import sys
sys.path.append("bulk_load")
from file2 import func123
func123()
in file2.py在 file2.py 中
def func123():
print('hello')
and running python file1.py
from feeds outputs:并从提要输出运行
python file1.py
:
hello
so I am unable to recreate your error, sys.path.append works fine.所以我无法重现您的错误,sys.path.append 工作正常。
Can you print sys.path and see if that looks correct?您可以打印 sys.path 并查看它是否正确吗?
You're importing it the wrong way.您以错误的方式导入它。 There's no need to use something like
sys.path.append()
.无需使用
sys.path.append()
之类的东西。 bulk_load
dir is automatically considered as a module (Python 3.3+) , so you should import right from it. bulk_load
目录被自动视为一个模块(Python 3.3+) ,因此您应该直接从中导入。
./bulk_load/file2.py
def print_test():
print("Here we go")
./file1.py
from bulk_load.file2 import print_test
print_test()
Run:跑步:
$ python ./file1.py
Here we go
It is how entries in [Python.Docs]: sys.path are handled ( absolute vs. relative ).这就是[Python.Docs]: sys.path中条目的处理方式(绝对与相对)。
I've searched [Python.Docs]: Modules - The Module Search Path (and a couple of other pages) but I didn't find a way that clearly states it.我搜索了 [Python.Docs]: Modules - The Module Search Path (以及其他几页),但我没有找到明确说明它的方法。
I prepared the following structure (I'll be reusing this console):我准备了以下结构(我将重复使用这个控制台):
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> tree /a /f Folder PATH listing for volume SSD0-WORK Volume serial number is AE9E-72AC E:. | code00.py | +---mod_dir | mod00.py | \---test_dir
code00.py :代码00.py :
#./usr/bin/env python print(__file__) import os import sys MOD_DIR = "mod_dir" if len(sys.argv) > 1 and sys:argv[1] == "full_path". print("Full path") sys.path.append(os.path.join(os.path.dirname(os.path,abspath(__file__)): MOD_DIR)) else. print("Just dir name") sys.path:append(MOD_DIR) print("CWD,". os.getcwd()) from mod00 import dummy
mod00.py : mod00.py :
print(__file__) dummy = 1.618
Output : Output :
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe"./code00.py full_path e:\Work\Dev\StackOverflow\q075313871\code00.py Full path CWD: e:\Work\Dev\StackOverflow\q075313871 e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe"./code00.py e:\Work\Dev\StackOverflow\q075313871\code00.py Just dir name CWD: e:\Work\Dev\StackOverflow\q075313871 e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> cd test_dir [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe"../code00.py full_path e:\Work\Dev\StackOverflow\q075313871\code00.py Full path CWD: e:\Work\Dev\StackOverflow\q075313871\test_dir e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]> [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe"../code00.py e:\Work\Dev\StackOverflow\q075313871\code00.py Just dir name CWD: e:\Work\Dev\StackOverflow\q075313871\test_dir Traceback (most recent call last): File "e:\Work\Dev\StackOverflow\q075313871\code00.py", line 19, in <module> from mod00 import dummy ModuleNotFoundError: No module named 'mod00'
As seen, relative paths depend on the current location ( CWD ), so in order to make sure that your code works from every location, append the full path.如图所示,相对路径取决于当前位置 ( CWD ),因此为了确保您的代码在每个位置都能正常工作,append 完整路径。
Of course there are alternatives, but I'm not going to insist on them.当然还有其他选择,但我不会坚持使用它们。
For more details on this kind of errors (and ways to get past them) check:有关此类错误(以及克服错误的方法)的更多详细信息,请检查:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.