Search code examples
pycharmcurses

how to import curses in pycharm


I can't use import curses in my PyCharm. I dont know the problem. What should I do?

I already added python -m pip install windows-curses in my cmd. But my code that contain 'import curses' got error. It says that no module named curses.

import random
import curses


s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = sw//4
snk_y = sh//2

this is the output error:

File "C:/Users/USER/PycharmProjects/snake/snake1", line 2, in <module>
        import curses

File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\curses\__init__.py", line 13, in <module>
        from _curses import *
    ModuleNotFoundError: No module named '_curses'

Solution

  • Probably you didn't choose the correct python interpreter in PyCharm.
    You can quickly check which python interpreter you are using in PyCharm clicking on:

    Settings -> Project -> Project Interpreter
    

    Here, in the top of the window, you should see the path and the name of project Interpreter that you are using. You can change using the drop-down select or if you need to add a new one, click on settings icon on the top-right corner of the window. Then click on Add and navigate to your virtualenv/python interpreter (remember that you have to choose the python file).
    Adding the interpreter, you should be able to use it, and so also your code should work well.

    In case you are already using the correct interpreter, you should modify the configuration that you are using to run the program. In order to do this, simply click on:

    Run -> Edit Configurations -> choose your configuration on the left
    

    Here, on the right side of the window (Configuration tab) you should be able to choose the Python Interpreter. Then click on Apply and Ok.