PyCharm error: 'No Module' when trying to import own module (python script)

前端 未结 14 1831
醉话见心
醉话见心 2020-12-07 07:57

I have written a module (a file my_mod.py file residing in the folder my_module). Currently, I am working in the file cool_script.py t

14条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 08:12

    The solution for this problem without having to Mark Directory as Source Root is to Edit Run Configurations and in Execution select the option "Redirect input from" and choose script you want to run. This works because it is then treated as if the script was run interactively in this directory. However Python will still mark the module name with an error "no module named x":

    When the interpreter executes the import statement, it searches for x.py in a list of directories assembled from the following sources:

    1. The directory from which the input script was run or the current directory if the interpreter is being run interactively
    2. The list of directories contained in the PYTHONPATH environment variable, if it is set.
    3. An installation-dependent list of directories configured at the time Python is installed, in my case usr/lib/python3.6 on Ubuntu.

提交回复
热议问题