Search code examples
pythonpippython-modulepython-packaging

Custom Python package is not accessible installed via Pip


I am writing a Python package and this is my file structure. simple_eda is main folder in which I have init file and my code file. In my code file I have Class SimpleEDA which does all the work. to import I want to use

import SimpleEDA or from simple_eda import SimpleEDA

My init file is empty.

  • simple_eda

    .init.py

    .simple_eda.py

  • tests

  • setup.py

  • README.md

  • LICENSE

I have used this command to build whl for my simple_eda. I have used this command in the main directory where setup.py file is located.

python3 setup.py sdist bdist_wheel

This created whl file and tar.gz file successfully in dist folder. So I used

pip install simple_eda.whl

and the package gets installed. so I write python in my terminal to activate Python. I can import my package user

from simple_eda.simple_eda import SimpleEDA

but If I try to do this in Jupyter notebooks, it gives me error.

 from simple_eda.simple_eda import SimpleEDA

Here is my setup.py file code.

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="simple_eda", 
    version="0.0.1",
    author="Muhammad Shahid Sharif",
    author_email="[email protected]",
    description="A wrapper around Pandas to perform Simple EDA with less code.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="my git link here",
    packages=['simple_eda'],
    install_requires = ['matplotlib==3.0.3','nltk==3.4.5',
'numpy==1.17.2',
'numpydoc==0.9.1',
'pandas==0.25.1',
'scikit-image==0.15.0',
'scikit-learn==0.22.2.post1',
'scipy==1.4.1',
'seaborn==0.9.0',
'spacy==2.2.3',
'spacy-langdetect==0.1.2',
'spacy-readability==1.3.0',
'textblob==0.15.3'],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.5',
)

I want to import my package like

import SimpleEDA or from simple_eda import SimpleEDA

Solution

  • The problem seems to be that OP has multiple conda environments and that the package was installed in one python environment, but jupyter notebook does not have access to that environment.

    To use jupyter notebook with multiple conda environments, the recommended practice is to install nb_conda_kernels in the base environment and then install ipykernel (or other language kernel) in every environment that should be usable in jupyter notebook.

    conda install -n base nb_conda_kernels
    conda install -n MYENV ipykernel
    jupyter-notebook  # Run this from the base environment
    

    Then, navigate to your jupyter notebook, open it up, choose the kernel that corresponds to the conda environment you want to use, and run your notebook.

    Also, to install the custom pip package in a certain conda environment, OP should be explicit with their commands. For example, use python -m pip instead of the pip wrapper.

    conda activate MYENV  # or source activate MYENV
    python -m pip install MYPACKAGE.whl
    

    In the OP's case, they should install ipykernel in whichever environment has their custom package, and then when using jupyter notebook, they should use that environment's kernel.

    Related: https://github.com/jupyter/help/issues/342#issuecomment-382837602