简体   繁体   中英

How do I replace import sys sys.path.append('./') from all my imports

I am using Vscode and every time I want to import a package from the current working directory, I need to have the following import at the top before importing my own module.

import sys
sys.path.append('./')

For example, my folder is:

My-Project
  folder1:
    - main.py
  folder2:
    - fun.py

main.py

import sys
sys.path.append('./')
from folder2 import fun

My question is how do I get rid of the import sys from the code?

The reason is that in VS Code, when importing other files in a file, it searches from the parent folder of the current file by default (for example: folder2 cannot be found in the file folder1). Therefore, the terminal displays that the folder cannot be found.

Since you don't want to use code to solve this problem, here is a method and you could refer to it.

Please use the debugging function of VS Code and add the following settings in "launch.json", which will add the project path to the system path when debugging:

"env": {
                "PYTHONPATH": "${workspaceFolder}",
            }

Then click F5 or " Start Debugging ":

在此处输入图像描述

I am using Vscode and every time I want to import a package from the current working directory, I need to have the following import at the top before importing my own module.

import sys
sys.path.append('./')

For example, my folder is:

My-Project
  folder1:
    - main.py
  folder2:
    - fun.py

main.py

import sys
sys.path.append('./')
from folder2 import fun

My question is how do I get rid of the import sys from the code?

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