There is no problem with automatic operation in pycharm, ImportError: No module named appears when running on the command line

The program can run in the development environment, but the command line will not work. The project directory is shown in the figure:

codes_no_useful_local is the project directory, A and B are two folders, there is a script test.py in the A directory, and a script func.py in the B directory. The code in test.py is to call the function in func.py, but it needs to be added The above and below codes can be executed through the command line, otherwise, the imported package cannot be found.

import  sys
import os

project = 'codes_no_useful_local'
sys.path.append(os.getcwd().split(project)[0]+project)
from B import func

func.b_func()

 

Guess you like

Origin blog.csdn.net/t20134297/article/details/108712723