Close Navigation
Learn more about IBKR accounts
Python Troubleshooting Q&A

Python Troubleshooting Q&A

Posted February 14, 2023
Sang-Heon Lee
SHLee AI Financial Model

This is just a small collection of common error messages and solutions that we encounter when using Python.

1. Pyfolio – AttributeError: ‘numpy.int64’ object has no attribute ‘to_pydatetime’

  • # remove your installed pyfolio library
    pip uninstall pyfolio
    # Install it again from its github repo
    pip install git+https://github.com/quantopian/pyfolio

2. For using git, add git path to the PATH on Windows

  • Find Git executable file (git.exe) is located
    in C:\Program Files\Git\bin
    or C:\Program Files\Git\cmd
    or C:\Users\XXX\AppData\Local\GitHubDesktop\app-3.0.6\resources\app\git\cmd
    or other directories

    The directory that is found must be added to the PATH environment variable and the version of Git needs to be confirmed as follows.

    C:\Users\shlee>git –version
    git version 2.35.4.windows.1

3. TabError: inconsistent use of tabs and spaces in indentation

  • This problem occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don’t mix the two in the same code block.

4. Multiple prints in Jupyter notebook

# InteractiveShell.ast_node_interactivity 
# : 'all' | 'last' | 'last_expr' | 'none' 
# (default : 'last_expr')
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

5. ValueError: Please initialize Bidirectional  layer with a tf.keras.layers.Layer instance.

Just delete tensorflow.

#from tensorflow.keras.layers import Bidirectional, Dropout, Activation, Dense, LSTM
#from tensorflow.python.keras.layers import CuDNNLSTM
#from tensorflow.keras.models import Sequential

from keras.layers import Bidirectional, Dropout, Activation, Dense, LSTM
from keras.layers import CuDNNLSTM
from keras.models import Sequential

6. Current directory of the running file

os.path.abspath(”)

7. set_random_seed() error

Q : ImportError: cannot import name ‘set_random_seed’ from ‘tensorflow’ (C:\Users\shlee\anaconda3\lib\site-packages\tensorflow\__init__.py)

A: TensorFlow API has been updated from set_random_seed() to set_seed()

#from tensorflow import set_random_seed
#set_random_seed(2)

from tensorflow.random import set_seed
set_seed(2)

Originally posted on SH Fintech Modeling Blog.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.