Welcome to see my homepage and contact me: NicholasYe’s Homepage.
Two lines to solve your python path problems in Vscode
-
Two lines like this:
import os, sys sys.path.append(os.getcwd()) -
Let me explain more clearly, assuming you have a project structured like this:
- Code
__init__.pyMain.py- Component
__init__.pyget_time.py
- Dataset
__init__.pydeal_formula.py
- Code
-
If you want to import some functions in or classes
Main.pyfromget_time.pyordeal_formula.py, here is how we do:import os, sys sys.path.append(os.getcwd()) from Code.Component import get_now_time() # Import get_now_time() function from Dataset.deal_formula import Formula # Import Formula class
本文介绍了如何在VScode中通过两行代码解决Python项目的导入路径问题。当你的项目结构复杂,如包含Code、Component和Dataset等子目录时,可以使用`import os, sys; sys.path.append(os.getcwd())`来添加当前工作目录到Python路径,从而顺利从Main.py导入get_time.py和deal_formula.py中的函数和类。
683

被折叠的 条评论
为什么被折叠?



