简体   繁体   English

自定义 Python package 无法通过 Pip 安装

[英]Custom Python package is not accessible installed via Pip

I am writing a Python package and this is my file structure.我正在写一个 Python package 这是我的文件结构。 simple_eda is main folder in which I have init file and my code file. simple_eda 是我有初始化文件和我的代码文件的主文件夹。 In my code file I have Class SimpleEDA which does all the work.在我的代码文件中,我有 Class SimpleEDA 可以完成所有工作。 to import I want to use导入我想使用

import SimpleEDA or from simple_eda import SimpleEDA

My init file is empty.我的初始化文件是空的。

  • simple_eda simple_eda

    . . init .py初始化.py

    .simple_eda.py .simple_eda.py

  • tests测试

  • setup.py安装程序.py

  • README.md自述文件.md

  • LICENSE执照

I have used this command to build whl for my simple_eda.我已使用此命令为我的 simple_eda 构建 whl。 I have used this command in the main directory where setup.py file is located.我在 setup.py 文件所在的主目录中使用了这个命令。

python3 setup.py sdist bdist_wheel

This created whl file and tar.gz file successfully in dist folder.这在 dist 文件夹中成功创建了 whl 文件和 tar.gz 文件。 So I used所以我用

pip install simple_eda.whl pip 安装 simple_eda.whl

and the package gets installed.并安装了 package。 so I write python in my terminal to activate Python.所以我在终端中写了 python 来激活 Python。 I can import my package user我可以导入我的 package 用户

from simple_eda.simple_eda import SimpleEDA

but If I try to do this in Jupyter notebooks, it gives me error.但如果我尝试在 Jupyter 笔记本中执行此操作,则会出现错误。

 from simple_eda.simple_eda import SimpleEDA

Here is my setup.py file code.这是我的 setup.py 文件代码。

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="chshahidhamdam@gmail.com",
    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我想导入我的 package

import SimpleEDA or from simple_eda import SimpleEDA

Since you are in a Conda environment, it appears you have not installed Jupyter for that environment.由于您处于 Conda 环境中,因此您似乎尚未为该环境安装 Jupyter。 Instead, the jupyter executable you are running is probably a globally installed one, while your home-grown package is installed locally (inside the Conda environment) with pip .相反,您正在运行的jupyter可执行文件可能是全局安装的,而您自己开发的 package 使用pip在本地(在 Conda 环境中)安装。 Note the different paths for the two executables:请注意两个可执行文件的不同路径:

  • Jupyter: /snap/bin/jupyter Jupyter: /snap/bin/jupyter
  • python3: $HOME/anaconda3/envs/eda_test_2/bin/python3 python3: $HOME/anaconda3/envs/eda_test_2/bin/python3

Thus, simply install Jupyter for your Conda environment, either with因此,只需为您的 Conda 环境安装 Jupyter,或者使用

conda install jupyter

or或者

pip install jupyter

Side note: since you are in a Conda environment, you should be able to simply use python instead of python3 .旁注:由于您处于 Conda 环境中,因此您应该能够简单地使用python而不是python3 One is just aliased to the other.一个只是另一个的别名。 This may not work outside your environment, if you are on an OS where python is still Python 2.如果您在python仍然是 Python 2 的操作系统上,这可能无法在您的环境之外工作。

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.问题似乎是 OP 有多个 conda 环境,并且 package 安装在一个 python 环境中,但 jupyter notebook 无法访问该环境。

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.要将 jupyter notebook 与多个 conda 环境一起使用,推荐的做法是在基础环境中安装nb_conda_kernels ,然后在应该在 jupyter notebook 中使用的每个环境中安装ipykernel (或其他语言内核)。

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.然后,导航到您的 jupyter notebook,打开它,选择与您要使用的 conda 环境相对应的 kernel,然后运行您的 notebook。

Also, to install the custom pip package in a certain conda environment, OP should be explicit with their commands.此外,要在某个 conda 环境中安装自定义 pip package,OP 的命令应该是明确的。 For example, use python -m pip instead of the pip wrapper.例如,使用python -m pip代替pip包装器。

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.在 OP 的情况下,他们应该在具有自定义 package 的任何环境中安装ipykernel ,然后在使用 jupyter notebook 时,他们应该使用该环境的 kernel。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM