# Egi library and python3

**URL:** <https://discourse.psychopy.org/t/egi-library-and-python3/6911>\
**Category:** Coding\
**Created:** [March 5, 2019, 8:19am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911 "2019-03-05T08:19:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jure\_Demsar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jure_demsar/32/4049_2.png) [@Jure\_Demsar](https://discourse.psychopy.org/u/Jure_Demsar)\
**Post date:** [March 5, 2019, 8:19am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/1 "2019-03-05T08:19:51Z")

</div>

Hi,

The egi (egi.simple) library works fine if I use a python2 version of PsychoPy (e.g. 1.90.3), but if I try to use a recent version (one that is built on python3) I get the following error:

```auto
Traceback (most recent call last):
  File "C:\gpsy3\tasks\re_oddball_egi.py", line 16, in <module>
    import egi.simple as egi
  File "C:\Program Files (x86)\PsychoPy3\lib\site-packages\egi\simple.py", line 347
    type(1L): ('long', '=l'), # '=' -- translation is: four bytes
          ^
SyntaxError: invalid syntax

```

Anyone has any ideas how to solve this, or is the egi library simple incompatible with python3?

Thanks!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [March 5, 2019, 12:01pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/2 "2019-03-05T12:01:18Z")

</div>

Yes, in Python 2 has two integer types, `int` and `long`. in Python 3, all integers are long integers:

[http://python3porting.com/differences.html#long](http://python3porting.com/differences.html#long)

To fix, the egi code will need to be made compatible for Python 3 installations i.e., not stating whether int is long. The link provided has potential fixes, and perhaps this can go on GitHub as an issue, considering @jon is the maintainer of egi:

> **[egi](https://pypi.org/project/egi/)**
>
> UNKNOWN

Would you like to attempt to fix and test this?

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [March 5, 2019, 12:38pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/3 "2019-03-05T12:38:54Z")

</div>

Here is a start, should address the issue in your original post. You would have to overwrite the existing version of simple.py in your psychopy installation. Try this and see if it solves your problem.

[simple.py](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/2/2915ca22aa77b37c7bbd7eeafa27f82e64dbaba0.py) (25.5 KB)

---

<div class="post-metadata">

**Author:** ![Jure\_Demsar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jure_demsar/32/4049_2.png) [@Jure\_Demsar](https://discourse.psychopy.org/u/Jure_Demsar)\
**Post date:** [March 5, 2019, 1:38pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/4 "2019-03-05T13:38:31Z")

</div>

Hi, I already tried something like this but gave up somewhere in the middle. Your .py file has mixed tabs and spaces for offsets, after fixing this I get the same error as before.

It seems like this python checks for this before running any of the code, the condition sys.version\_info[0] \< 3 never gets checked, because errors are now in pieces of code that python3 should never get to (since sys.version\_info[0] == 3).

If I remove all the code that is meant for pythons lower than 3 and only keep the two pieces that run if sys version is 3 or greater, I get a different error:

```auto
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/egi/simple.py", line 4, in <module>
    from socket_wrapper import Socket
ModuleNotFoundError: No module named 'socket_wrapper'

Cheers!

```

---

<div class="post-metadata">

**Author:** ![Jure\_Demsar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jure_demsar/32/4049_2.png) [@Jure\_Demsar](https://discourse.psychopy.org/u/Jure_Demsar)\
**Post date:** [March 5, 2019, 1:40pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/5 "2019-03-05T13:40:26Z")

</div>

Ah I forgot to change from socket\_wrapper import Socket to from .socket\_wrapper import Socket as this is python3 now.

---

<div class="post-metadata">

**Author:** ![Jure\_Demsar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jure_demsar/32/4049_2.png) [@Jure\_Demsar](https://discourse.psychopy.org/u/Jure_Demsar)\
**Post date:** [March 5, 2019, 1:43pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/6 "2019-03-05T13:43:23Z")

</div>

It imports now, I will try running it on a NetStation later this week, will let you know how it goes.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [March 5, 2019, 1:55pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/7 "2019-03-05T13:55:13Z")

</div>

Great thanks. Yes, apologies for the formatting error, I was not using my normal text editor 🙂

---

<div class="post-metadata">

**Author:** ![Jure\_Demsar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jure_demsar/32/4049_2.png) [@Jure\_Demsar](https://discourse.psychopy.org/u/Jure_Demsar)\
**Post date:** [March 14, 2019, 9:39am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/8 "2019-03-14T09:39:44Z")

</div>

I tried to run the experiment today with the manually updated library but it does not work. The experiment crashes when I try to start the session via net station:

```auto
Traceback (most recent call last):
  File "C:\gpsy3\tasks\oddball_egi.py", line 207, in <module>
    net_station.BeginSession
  File "..\libs\egi\simple.py", line 641, in BeginSession
    message = self._fmt.pack('Q', self._system_spec)
  File "..\libs\egi\simple.py", line 248, in pack
    result = struct.pack(fmt, key, *args)
struct.error: char format requires a bytes object of length 1

```

I raised an issue on the [https://github.com/gaelen/python-egi](https://github.com/gaelen/python-egi), until it gets resolved we will just use an old version of PsychoPy (everything works as it should on 1.90.3).

---

<div class="post-metadata">

**Author:** ![nrh31](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@nrh31](https://discourse.psychopy.org/u/nrh31)\
**Post date:** [November 21, 2019, 3:45pm UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/9 "2019-11-21T15:45:00Z")

</div>

Has anyone made any progress with this issue? I am running into the same problem with trying to run egi library with python 3 @gstothart

---

<div class="post-metadata">

**Author:** ![gstothart](https://avatars.discourse-cdn.com/v4/letter/g/d78d45/32.png) [@gstothart](https://discourse.psychopy.org/u/gstothart)\
**Post date:** [February 28, 2020, 9:01am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/10 "2020-02-28T09:01:14Z")

</div>

Hi everyone, just a heads up that we have nearly finished updating the egi library to work with Python 3. We’ve got it working and are just documenting all the changes we’ve made. I’ll post it up here hopefully in the next week or so, @jon you’re welcome to include it in the new releases of Psychopy.  
Cheers  
George

---

<div class="post-metadata">

**Author:** ![koherenssi](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@koherenssi](https://discourse.psychopy.org/u/koherenssi)\
**Post date:** [March 30, 2020, 11:02am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/11 "2020-03-30T11:02:11Z")

</div>

What is the status of egi python 3 update mentioned in above post?

---

<div class="post-metadata">

**Author:** ![gstothart](https://avatars.discourse-cdn.com/v4/letter/g/d78d45/32.png) [@gstothart](https://discourse.psychopy.org/u/gstothart)\
**Post date:** [March 31, 2020, 11:08am UTC](https://discourse.psychopy.org/t/egi-library-and-python3/6911/12 "2020-03-31T11:08:04Z")

</div>

Hi, we’ve just put it up on Github, see [https://github.com/gstothart/EGI-Psychopy-Python3](https://github.com/gstothart/EGI-Psychopy-Python3)
