Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import own module in PyCharm console

Tags:

python

pycharm

I have an own module in my project directory and I import it into my code.

main.py:

from my_module import Test

print(Test.test())

my_module.py:

class Test:
@staticmethod
def test():
    return '123'

There is no problem running the code in PyCharm. But when I try to "Execute Selection in Console", I get

Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pydev\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) ImportError: No module named 'my_module'

How do I import own modules in the PyCharm console?

like image 455
Thomas Sablik Avatar asked Apr 12 '16 22:04

Thomas Sablik


1 Answers

You can also instruct PyCharm to add source roots to PYTHONPATH in the Python Console:

  • go to File -> Settings (or Default Settings) -> Build, Execution, Deployment -> Console -> Python Console
  • check "Add source roots to PYTHONPATH".

For some reason, this option is not activated by default.

like image 120
ochedru Avatar answered Sep 26 '22 14:09

ochedru