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

前端 未结 14 1788
醉话见心
醉话见心 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:30

    What I tried is to source the location where my files are.

    e.g. E:\git_projects\My_project\__init__.py is my location.

    I went to File -> Setting -> Project:My_project -> Project Structure and added the content root to about mention place E:\git_projects\My_project

    it worked for me.

    0 讨论(0)
  • 2020-12-07 08:32

    ln -s . someProject

    If you have someDirectory/someProjectDir and two files, file1.py and file2.py, and file1.py tries to import with this line

    from someProjectDir import file2

    It won't work, even if you have designated the someProjectDir as a source directory, and even if it shows in preferences, project, project structure menu as a content root. The only way it will work is by linking the project as show above (unix command, works in mac, not sure of use or syntax for Windows). There seems some mechanism where Pycharm does this automatically either in checkout from version control or adding as context root, since the soft link was created by Pycharm in a dependent project. Hence, just copying the same, although the weird replication of directory is annoying and necessity is perplexing. Also in the dependency where auto created, it doesn't show as new directory under version control. Perhaps comparison of .idea files will reveal more.

    0 讨论(0)
提交回复
热议问题