0

Hi i am having trouble importing backtrader and IbPy2. When i pip install both and when i run import backtrader in my python shell i get the following error:

i pip installed these:

pip install https://github.com/blampe/IbPy/archive/master.zip

pip install backtrader

The error i encountered:

Traceback (most recent call last):

  File ~/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3369 in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  Input In [3] in <cell line: 1>
    import backtrader as bt

  File ~/opt/anaconda3/lib/python3.9/site-packages/backtrader/__init__.py:62 in <module>
    from .cerebro import *

  File ~/opt/anaconda3/lib/python3.9/site-packages/backtrader/cerebro.py:35 in <module>
    from .brokers import BackBroker

  File ~/opt/anaconda3/lib/python3.9/site-packages/backtrader/brokers/__init__.py:30 in <module>
    from .ibbroker import IBBroker

  File ~/opt/anaconda3/lib/python3.9/site-packages/backtrader/brokers/ibbroker.py:30 in <module>
    import ib.ext.Order

  File ~/opt/anaconda3/lib/python3.9/site-packages/ib/ext/Order.py:9 in <module>
    from ib.lib import Double, Integer

  File ~/opt/anaconda3/lib/python3.9/site-packages/ib/lib/__init__.py:239
    except (socket.error, ), ex:
                           ^
SyntaxError: invalid syntax

The specs of my machine are:

  • macbook air m1
  • jupyterlabs
  • anaconda

I think i pretty much narrowed the problem down to IbPy2's fault. Whenever i import backtrader with no IbPy2 installed it works fine. But soon as IbPy2 is installed using the above pip command i encounter this error.

Any help is appreciated and would make my week I have been stuck for days! Thank you in advance!

James
  • 15
  • 5

1 Answers1

1

pip install wheel

Explanation:

  • What is the error: It is a python2 syntax error because it runs on different version of python. Please read this answer for detail. Python 2 and Python 3.

  • Why does it occur: Because there is no wheel installed, especially when you create a virtual environment with pipenv. Pipenv or pip installs IbPy2 without wheel and the message is like this: Using legacy 'setup.py install' for IbPy2, since package 'wheel' is not installed. And then it was recognized as the code written in Python2.

  • How to solve: Install wheel first with pip install wheel. And then install IbPy2 with pip install IbPy2, then it will be properly installed and recognized as Python3 code. So you will not see the Python2 syntax error anymore.