Skip to content

Debugging with PyCharm throws error when environment variable not specified #1426

@tworedz

Description

@tworedz

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

Hi OpenAI team!

I'm having trouble debugging my project that uses the openai package along with several other dependencies. I'm encountering an exception during the import process.

Here is my file t.py:

from openai import OpenAI

Running script in debug mode in PyCharm:

python t.py

Initially, I attempted to execute uvicorn with FastAPI, but the issue persists even when they are not involved.

Here is traceback:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1534, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/tworedz/workspace/temp/language_model/t.py", line 1, in <module>
    from openai import OpenAI
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/__init__.py", line 345, in <module>
    from ._module_client import (
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/_module_client.py", line 75, in <module>
    chat: resources.Chat = ChatProxy().__as_proxied__()
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "_pydevd_bundle/pydevd_pep_669_tracing_cython.pyx", line 504, in _pydevd_bundle.pydevd_pep_669_tracing_cython.PyRaiseCallback.__call__
  File "_pydevd_bundle/pydevd_pep_669_tracing_cython.pyx", line 47, in _pydevd_bundle.pydevd_pep_669_tracing_cython.PEP669CallbackBase.frame
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/_utils/_proxy.py", line 49, in __class__
    proxied = self.__get_proxied__()
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/_utils/_proxy.py", line 55, in __get_proxied__
    return self.__load__()
           ^^^^^^^^^^^^^^^
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/_module_client.py", line 12, in __load__
    return _load_client().chat
           ^^^^^^^^^^^^^^
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/__init__.py", line 323, in _load_client
    _client = _ModuleClient(
              ^^^^^^^^^^^^^^
  File "/Users/tworedz/Library/Caches/pypoetry/virtualenvs/language-model-5UFyvxqf-py3.12/lib/python3.12/site-packages/openai/_client.py", line 104, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

By the way, the issue doesn't occur if we specify the OPENAI_API_KEY environment variable. However, it's peculiar that simply importing the package initializes a class with side effects, and this only happens in debug mode in PyCharm.

I'm using PyCharm 2023.3.5 (Professional Edition) on macOS Sonoma M1 Pro.

To Reproduce

Described steps above.

Code snippets

No response

OS

macOS

Python version

Python 3.12.3

Library version

openai 1.30.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions