Search code examples
pythonopenai-apilarge-language-model

How to import openai package using jupyter notebook?


I am getting the below error message when importing openai as ai using google jupyter notebook:

ImportError                               Traceback (most recent call last)
<ipython-input-9-3f86bb4abbfc> in <module>
----> 1 import openai as ai

   /opt/anaconda3/lib/python3.8/site-packages/openai/__init__.py in <module>
      4 
      5 import os as _os
----> 6 from typing_extensions import override
      7 
      8 from . import types

ImportError: cannot import name 'override' from 'typing_extensions' 
(/opt/anaconda3/lib/python3.8/site-packages/typing_extensions.py)

I have no idea how to fix this. Any ideas?


Solution

  • It seems a problem related to versions that seem incompatible.

    I tested a Python 3.8 environment and openai==1.6.1 seems to work fine. Try to install the following pinned version.

    python -m pip install openai==1.6.1
    

    EDIT:

    pd: typing_extension is well known to cause a lot of dependcy problems, as this one. I suggest you:

    1. Pin your version in a requirements.txt to ensure your project work as expected. Here are mines from a recent project. If the above suggestion does not work, use this snippet as the requirements.txt.
    python-dotenv~=1.0.0
    azure-core~=1.29.5
    azure-identity~=1.15.0
    chainlit~=0.7.604
    openai~=1.3.5
    langchain~=0.0.339
    pypdf~=3.17.1
    python-docx~=1.1.0
    faiss-cpu
    tiktoken
    pysqlite3-binary
    
    1. It is almost sure that you will face similar issues soon. So, I suggest you to search the Error trace on Google and dig into the forums (Github issues, Stackoverflow, etc) for answers. For example, you could type the following in google:
    ImportError: cannot import name 'override' from 'typing_extensions'
    

    And you would find this Giuthub issue: https://github.com/openai/openai-python/issues/751 which could help you clarify what it is happening.