Home > Blockchain >  How to run a python script with modules inside?
How to run a python script with modules inside?

Time:04-13

I have a test.py file that I try to run in VSCode with the "Run" button. I have a string from file_folder.file import something inside it. When I try to run my test, VSCode gives me an error ModuleNotFoundError: No module named 'file_folder'.

My project tree looks something like this:

root_folder -- file_folder -- file.py
            -- test_folder -- my_folder -- test.py

Can I make it work in VSCode somehow?

CodePudding user response:

You can try something like this:

import sys
sys.path.insert(1, '<pathToFolder>/file_folder')
from file import something 

CodePudding user response:

For cross directory access, we usually use relative paths to facilitate code portability.

import sys
from os.path import dirname, abspath

project_path = dirname(dirname(abspath(__file__)))
sys.path.append(project_path   "\\file_folder")
from file import *
  • Related