0

I created a straightforward Python script :

import pandas # as pd unnecessary

class dataset:
    """
        The dataset class aims to take a dataset, define a target and
    draw all descriptive statistics into a Python widget.

    INPUT : df, a Pandas dataframe
            target, a string denoting a column
    """

    def __init__(self,df,target):
        self.df = df
        self.target = target
    
        # Checking errors
        if ~isinstance(df, pandas.core.frame.DataFrame):
            raise TypeError("df is not a Pandas dataframe.")
        if ~isinstance(target,str):
            raise TypeError("target is not a string variable.")
        if target not in df.columns:
            raise ValueError("The target is not available in the dataframe.")

Yet when I want to import it in a Jupiter notebook this way (the two scripts are in the same folder, the one above being named datavizToolbox.py) :

from datavizToolbox import dataset

It yields that it is unable to import :

ImportError: cannot import name 'dataset' from 'datavizToolbox' (* My path *)

Is there something I am missing ?

GaëtanLF
  • 101
  • 10
  • 1
    That script clearly does have an item named `dataset`, so there must be something else wrong here. I suspect you have a module in some other location that is also named `datavizToolbox`, and that one is being imported instead of the one you intended. Try this code in Jupyter and show us the output: `import datavizToolbox; print(datavizToolbox.__path__)` – John Gordon Sep 25 '22 at 16:06
  • 1
    Note that `~` is bitwise not. You need boolean not. `~True` is `-2` and `~False` is `-1`. You will always raise TypeError – buran Sep 25 '22 at 16:12
  • 3
    Does this answer your question? ["ImportError: No module named" when trying to run Python script](https://stackoverflow.com/questions/15514593/importerror-no-module-named-when-trying-to-run-python-script) – Tom McLean Sep 25 '22 at 16:15
  • @JohnGordon it tells me that "module 'datavizToolbox' has no attribute '__path__'" – GaëtanLF Sep 25 '22 at 16:17
  • 1
    try `datavizToolbox.__file__` instead of `__path__` – furas Sep 25 '22 at 16:20
  • @TomMcLean it does not seem to work ... – GaëtanLF Sep 25 '22 at 16:21
  • Thanks @furas, it sends me back the good path file for my module – GaëtanLF Sep 25 '22 at 16:22
  • 1
    code may run from different folder (different `Current Working Directory`) which you can check with `os.getcwd()` - and this may need to add your folder to list `sys.path` before importing. – furas Sep 25 '22 at 16:22
  • 1
    always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Sep 25 '22 at 16:23
  • 1
    BTW: you can also use `print( open(datavizToolbox.__file__).read() )` to see what is in imported file. – furas Sep 25 '22 at 16:24
  • 1
    Try `print(dir(datavizToolbox))` – John Gordon Sep 25 '22 at 17:04
  • Thanks everyone, I closed and opened back all files and now it works ... that's probably a directory issue as you suggested. – GaëtanLF Sep 25 '22 at 19:17

0 Answers0