For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Cannot import backtrader, issue at __init__



  • In trying to run one of the simple examples to get started I am welcomed by the following error message. I was wondering if anybody could please check their _ init _ file to see if theirs matches mine, or if anybody has an idea of how to fix this to please let me know. Thank you.

    ImportErrorTraceback (most recent call last)
    <ipython-input-9-1a2598f2bead> in <module>()
    9
    10 # Import the backtrader platform
    ---> 11 import backtrader as bt
    12 import backtrader.feeds as btfeeds
    13

    C:\Users\Guwop\Anaconda3\envs\py27\lib\site-packages\backtrader_init_.py in <module>()
    24 from .version import version, btversion
    25
    ---> 26 from . import errors as errors
    27 from .errors import *
    28

    ImportError: cannot import name errors

    I'd also like to add that if I omit the problematic line, I receive another error of the same nature further down the file from the line:
    from . import broker as broker


  • administrators

    Whether someone else's __init__ matches yours or not plays no role (your actual __init__ file is in any case not shown)

    Things you can provide/try:

    • How did you install backtrader?
    • Output of pip list | grep backtrader
    • If you installed backtrader in the shell, is it the same version which is running in ipython?
    • What's the name of the script you are trying to execute?
    • What's the content of the script you are trying to execute?


  • @backtrader Thanks for responding quickly,

    I installed backtrader via the anaconda command prompt using pip for python 2.7 (which is an additional environment to my anaconda version, and yes my ipython is running on the same environment)

    Would you mind clarifying what the second bullet refers to?

    The script that I'm trying to execute comes from the quickstart page of backtrader.com. I made a minor change to try to feed a Pandas df but I don't imagine that's the issue as of yet since my import statements are at the top of the kernel.


  • administrators

    @guwop said in Cannot import backtrader, issue at __init__:

    Would you mind clarifying what the second bullet refers to?

    That one uses pip to show the list of installed packages and filters (greps) the name backtrader which will also show the version which is installed.

    @guwop said in Cannot import backtrader, issue at __init__:

    (which is an additional environment to my anaconda version, and yes my ipython is running on the same environment)

    Some others believed this to be true in the past and was proven wrong. It doesn't have to be your case, but it happens and it seems something is completely broken in your installation.



  • @backtrader, from pip list it says that the backtrader version is 1.9.65.122

    Regarding the anaconda environment, are you suggesting that possibly it's not actually running in 2.7? I make sure to change the environment in anaconda navigator and start ipython in such.

    Also, I omitted the from . import broker as broker line and next receive an error for
    from . import utils as utils
    so it seems as though it's having an issue importing whenever it's calling an instance of "from . " where the period is not connected to anything. Hopefully that can help too.



  • @backtrader , I went ahead and pip installed backtrader to my root version for anaconda, rebooted ipython, and everything seems to be working now. Thank you so much for bringing that up!