6

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?

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • Check [this](https://stackoverflow.com/questions/24197970/pycharm-import-external-library) out. Do `import sys` and `print(sys.path)` to see the current paths that will be searched when looking for imports. – jDo Apr 12 '16 at 22:08
  • `['C:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.4\\helpers\\pydev', 'C:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.4\\helpers\\pydev', 'C:\\WINDOWS\\SYSTEM32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages']` – Thomas Sablik Apr 12 '16 at 22:17
  • [A few options](http://stackoverflow.com/a/21488010/6004486) – jDo Apr 12 '16 at 22:36
  • 1
    You could also do `import sys`, `sys.path.append("/full/path/to/folder/containing/my_module.py")` – jDo Apr 12 '16 at 22:45
  • 2
    I only tried `import sys`, `sys.path.append("/full/path/to/folder/containing/my_module.py")` and it worked. – Thomas Sablik Apr 14 '16 at 08:10

4 Answers4

15

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.

Derlin
  • 9,572
  • 2
  • 32
  • 53
ochedru
  • 826
  • 8
  • 17
2

I don't use PyCharm but the problem is caused by environment variables like PATH that aren't necessarily available from within a program/IDE.

How to fix it properly/permanently has been discussed numerous times; e.g. here and here. Often, running the program from terminal fixes the issue because the program thereby "inherits" the environment variables. Another way is to use this quick fix:

import sys
sys.path.append("/full/path/to/folder/containing/your_module.py")
# Now, this should work:
import your_module
jDo
  • 3,962
  • 1
  • 11
  • 30
2

What worked for me was the following:

  1. Go to File -> Settings (or Default Settings) -> Build, Execution, Deployment -> Console -> Python Console
  2. Comment out "sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])"
  3. Now I can import modules inside the source roots in the console!
JohnnyQ
  • 425
  • 4
  • 16
2
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\\Users\\ahmet\\source\\repos\\python-core'])

PyCharm adds the root directory for you.

To import a file, you need to prefix the sub-directory name if any as follows

from subdirectory.myfile from ClassA
Ahmet Emrebas
  • 566
  • 6
  • 10
  • Somehow commenting out the sys.path line as @JohnnyQ suggested, then deleting the subdirectory solved my issue. I am not sure why my experience is opposite of you... – Stack_Protégé Mar 06 '21 at 05:25