Open gDanzel opened 3 weeks ago
Hey there, @gDanzel! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together!
The issue you're encountering is due to a misinterpretation of the import statement for the random
module in the _check_imports
function. The function incorrectly adds the import as {"module": "random", "name": "random", "alias": "random"}
. This leads to the get_environment
function using getattr
on the random
module, which results in the AttributeError
because it tries to access random.random
instead of the random
module itself.
To resolve this, you need to adjust the logic in the _check_imports
function to correctly handle the import of the random
module. Specifically, ensure that the name
field in the dependency dictionary reflects the correct attribute or module being imported. Here's a potential fix:
Modify the _check_imports
function to correctly handle the import of the random
module by ensuring that the name
field is not set to "random"
when importing the module itself.
Ensure that the get_environment
function correctly imports the module without using getattr
when the entire module is needed.
This adjustment should prevent the AttributeError
by ensuring that the random
module is imported correctly and that its methods, like choice
, are accessible.
To continue talking to Dosu, mention @dosu.
System Info
OS version: WIN11 Python version: 3.12 The current version of pandasai being used: 2.3.0
🐛 Describe the bug
I'm requesting LLM to generate plotly plot that able to randomly pick a template, and it returns the code using random module, the code snipet like:
But got error at code execution
error message:
I did a simple debug and found it's caused by wronly importing the random.random function instead of random module, the random function dose not have choice attribute cuased the error.
in _check_imports function of code_cleaning.py, it parsed the 'import random', to dependency dic as {"module": "random", "name": "random", "alias": "random"},
then later in code cleaning, the function get_environment of optional.py, for dependency 'random', it gose getattr(import_dependency(lib["module"]), lib["name"]) instead of import_dependency(lib["module"]), because if hasattr(import_dependency(lib["module"]), lib["name"]) is true for {"module": "random", "name": "random", "alias": "random"}.
...... lib["alias"]: ( getattr(import_dependency(lib["module"]), lib["name"]) if hasattr(import_dependency(lib["module"]), lib["name"]) else import_dependency(lib["module"]) ) ......
This is a bug that could potential cause problem handling import libs.