Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
tworedz opened this issue May 15, 2024 · 3 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@tworedz
Copy link

tworedz commented May 15, 2024

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

@tworedz tworedz added the bug Something isn't working label May 15, 2024
@maxupp
Copy link

maxupp commented May 15, 2024

Having this exact same behavior.

@maxupp
Copy link

maxupp commented May 15, 2024

@tworedz Updateing Pycharm fixed it for me.

@tworedz
Copy link
Author

tworedz commented May 20, 2024

Got it, thanks!

@tworedz tworedz closed this as completed May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants