# Psychopy on pycharm

**URL:** https://discourse.psychopy.org/t/psychopy-on-pycharm/26714
**Category:** Coding
**Tags:** screen, images, pycharm
**Created:** [January 6, 2022, 6:22pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714 "2022-01-06T18:22:16Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Zahra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zahra/32/33204_2.png) [@Zahra](https://discourse.psychopy.org/u/Zahra)
#### Post date: [January 6, 2022, 6:22pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/1 "2022-01-06T18:22:16Z")

</div>

I’m trying to control the position of an image stimuli for my experiment on PyCharm.  
The stimulus window appears only for a second and then disappears, and it does not take any keyboard input.  
Does working with PsychoPy on PyCharm need any specific arrangements?

This is my code:

```auto
from psychopy import core, event, visual, monitors, clock
import psychopy.event
# monitor
widthPix = 1920 # screen width in px
heightPix = 1080 # screen height in px
monitorwidth = 53.1 # monitor width in cm
viewdist = 60. # viewing distance in cm
monitorname = 'monitorname'
Screen = 0 # 0 to use main screen, 1 to use external screen
mon = monitors.Monitor(monitorname, width=monitorwidth, distance=viewdist)
mon.setSizePix((widthPix, heightPix))
mon.save()

# Initialize window
win = visual.Window(
    monitor=mon,
    size=(400, 400),
    units='pix',
    screen=Screen,
    allowGUI=True,
    fullscr=False)

period = psychopy.core.StaticPeriod(screenHz=60)

image = 'C:/Users/CVBE/PycharmProjects/spaceship_in_psychopy/spaceship.png'

image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))

image_stim.draw()
# text_stim.draw()
win.flip()
# period.complete()

keys = psychopy.event.waitKeys()
text_stim = visual.TextStim(
    win,
    text=keys,
    pos=(0.0, 0.0),
    units="norm",
    height=0.05,
    wrapWidth=0.8,
)
# period.start(2)

# image_stim.draw()
text_stim.draw()

if keys == 'right':
    print(keys)
    image_stim = visual.ImageStim(win, image, pos=(50, 0.0))
    text_stim.draw()
    image_stim.draw()
    win.flip()
    period.complete()
    win.close()
elif keys == 'left':
    print(keys)
    image_stim = visual.ImageStim(win, image, pos=(-1.0, 0.0))
    image_stim.draw()
    win.flip()
elif keys == 'up':
    print(keys)
    image_stim = visual.ImageStim(win, image, pos=(0.0, 1.0))
    image_stim.draw()
    win.flip()
elif keys == 'down':
    print(keys)
    image_stim = visual.ImageStim(win, image, pos=(0.0, -1.0))
    image_stim.draw()
    win.flip()
else:
    image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))
    image_stim.draw()
    win.flip()

```

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [January 6, 2022, 6:39pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/2 "2022-01-06T18:39:28Z")

</div>

I’m not an expert on PyCharm but there are a comparatively small number of posts about it. I’d recommend you search the forum and see if any of the previous issues and solutions can help you.

---

<div class="post-metadata">

### Author: ![Zahra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zahra/32/33204_2.png) [@Zahra](https://discourse.psychopy.org/u/Zahra)
#### Post date: [January 6, 2022, 9:07pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/3 "2022-01-06T21:07:53Z")

</div>

May I ask if the code I shared works for you? Does it take the keyboard input to move the image to right/left/etc? (You can use a sample image of your own)

---

<div class="post-metadata">

### Author: ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)
#### Post date: [January 6, 2022, 11:00pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/4 "2022-01-06T23:00:06Z")

</div>

Hi @Zahra,  
if the information from the issue linked below are still accurate then running PsychoPy from pycharm may be problematic.

> [@How do I get PsychoPy working in PyCharm on mac?](https://discourse.psychopy.org/t/how-do-i-get-psychopy-working-in-pycharm-on-mac/4088):
>
> I feel very stupid but I can’t get PyCharm to run a PsychoPy script for anything, and every time I try to load the interpreter in PyCharm it throws a fit. I know the correct interpreter (/Applications/PsychoPy2.app/Contents/MacOS/python), but anytime I try to do anything with it I get: from psychopy import visual, event, core, data, gui, monitors, tools, sound File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psychopy/\_\_init\_\_.py", line 43, in \<module\> …

You may try installing psychopy via conda (from the conda-forge channel) and trying running the pycharm script in that environment. Did you try running your script in PsychoPy Coder?

---

<div class="post-metadata">

### Author: ![Zahra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zahra/32/33204_2.png) [@Zahra](https://discourse.psychopy.org/u/Zahra)
#### Post date: [January 7, 2022, 9:14am UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/5 "2022-01-07T09:14:02Z")

</div>

Thanks @mmagnuski for your reply.  
On Mac, I brewed Psychopy, but importing it into PyCharm wasn’t possible.  
On Windows (11pro), I pip installed PsychoPy and I could import it into PyCharm. I haven’t used the Coder; I have coded it into a PyCharm script. It does create the stimuli that I code, but it does not take over the screen. It only shows the stimuli in a small window which disappears as soon as I press any key. I need to know if the disappearing is because I have coded it wrong, or it’s PyCharm’s problem.

---

<div class="post-metadata">

### Author: ![suelynnmah](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@suelynnmah](https://discourse.psychopy.org/u/suelynnmah)
#### Post date: [January 7, 2022, 10:26am UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/6 "2022-01-07T10:26:15Z")

</div>

Hi Zahra, I assume you’re not planning on putting your task online since you’re working from PyCharm? I have a builder task that can move an image with keyboard that works locally and in full screen, and you can take a look at the python script that can be generated from the task. Hopefully this will give you some ideas on how to proceed in PyCharm 🙂

![green square](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/8/18d7ec995545e45974603b4d35628ff7b94843cc.png)  
[moveSquare.psyexp](https://discourse.psychopy.org/uploads/short-url/q5J1eCi5O3NGFKAVkzJiaEx0kNA.psyexp) (10.1 KB)

---

<div class="post-metadata">

### Author: ![Zahra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zahra/32/33204_2.png) [@Zahra](https://discourse.psychopy.org/u/Zahra)
#### Post date: [January 7, 2022, 3:47pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/7 "2022-01-07T15:47:15Z")

</div>

Thanks a lot @suelynnmah this has helped me a lot!  
I have to add much more features to it for my experiment, but it’s great for a start.

---

<div class="post-metadata">

### Author: ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)
#### Post date: [January 8, 2022, 3:02pm UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/8 "2022-01-08T15:02:30Z")

</div>

After reading your code I have the following suggestions:

1. IIRC `psychopy.event.waitKeys()` returns a **list of keys** , not a single key, so you could change the waitKeys to:

```python
key = psychopy.event.waitKeys()[0]

```

or you could later not test for equality (`keys == 'right'`) but membership (`‘right’ in keys).
2. after checking the pressed key and flipping the window you should add another `waitKeys()` or at least something like `psychopy.core.wait(2)` - otherwise the window will likely close before you notice anything.
3. there is potential for refactor of the part where you check the pressed key, for example:

```python
image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))
key_to_pos = {'right': (1, 0), 'left': (-1, 0), 'up': (0, 1),
              'down': (0.0, -1.0)}

if key in key_to_pos.keys():
    image_stim.pos = key_to_pos[key]

image_stim.draw()
win.flip()
psychopy.event.waitKeys()

```

4. I don’t think you need the StaticPeriod in the example you show.

But generally: to test if there is a problem in your code or the IDE you are using has problems running PsychoPy - just try testing your code using PsychoPy runner. 🙂

---

<div class="post-metadata">

### Author: ![Zahra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zahra/32/33204_2.png) [@Zahra](https://discourse.psychopy.org/u/Zahra)
#### Post date: [January 21, 2022, 10:10am UTC](https://discourse.psychopy.org/t/psychopy-on-pycharm/26714/9 "2022-01-21T10:10:56Z")

</div>

thanks a lot @mmagnuski for the useful suggestions! sorry I hadn’t checked here in a while because of illness. Your suggestions helped a lot!
