Python 3.x 如何让Sphinx将熊猫识别为一个模块
我正在使用Python 3.x 如何让Sphinx将熊猫识别为一个模块,python-3.x,pandas,python-sphinx,Python 3.x,Pandas,Python Sphinx,我正在使用python3.9.2和sphinxbuild3.5.2。我已经创建了一个具有以下目录和文件结构的项目 core_utilities |_core_utilities | |_ __init__.py | |_read_files.py |_docs |_ sphinx |_Makefile |_conf.pg |_source | |_conf.py
python3.9.2
和sphinxbuild3.5.2
。我已经创建了一个具有以下目录和文件结构的项目
core_utilities
|_core_utilities
| |_ __init__.py
| |_read_files.py
|_docs
|_ sphinx
|_Makefile
|_conf.pg
|_source
| |_conf.py
| |_index.rst
| |_read_files.rst
|_build
read_files.py
文件包含以下代码。注意:我简化了这个例子,这样它就不会有分散注意力的信息。在这段代码中,有一个类包含一个成员函数,用于读取文本文件、查找关键字并将关键字右侧的变量作为numpy.float32
变量读取。编写的独立函数,用于读取具有特定数据类型的csv文件,并将其保存到数据帧中
# Import packages here
import os
import sys
import numpy as np
import pandas as pd
from typing import List
class ReadTextFileKeywords:
def __init__(self, file_name: str):
self.file_name = file_name
if not os.path.isfile(file_name):
sys.exit('{}{}{}'.format('FATAL ERROR: ', file_name, ' does not exist'))
# ----------------------------------------------------------------------------
def read_double(self, key_words: str) -> np.float64:
values = self.read_sentence(key_words)
values = values.split()
return np.float64(values[0])
# ================================================================================
# ================================================================================
def read_csv_columns_by_headers(file_name: str, headers: List[str],
data_type: List[type],
if not os.path.isfile(file_name):
sys.exit('{}{}{}'.format('FATAL ERROR: ', file_name, ' does not exist'))
dat = dict(zip(headers, data_type))
df = pd.read_csv(file_name, usecols=headers, dtype=dat, skiprows=skip)
return df
conf.py文件包含以下信息
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../../core_utilities'))
# -- Project information -----------------------------------------------------
project = 'Core Utilities'
copyright = 'my copyright'
author = 'my name'
# The full version, including alpha/beta/rc tags
release = '0.1.0'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc',
'sphinx.ext.autosummary', 'sphinx.ext.githubpages']
autodoc_member_order = 'groupwise'
autodoc_default_flags = ['members', 'show-inheritance']
autosummary_generate = True
autodock_mock_imports = ['numpy']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'nature'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
index.rst
文件包含以下信息
. Core Utilities documentation master file, created by
sphinx-quickstart
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Core Utilities's documentation!
==========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
read_files
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
并且read_files.rst
文件具有以下信息
**********
read_files
**********
The ``read_files`` module contains methods and classes that allow a user to read different
types of files in different ways. Within the module a user will find functionality that
allows them to read text files and csv files by columns and keywords. This module
also contains functions to read sqlite databases, xml files, json files, yaml files,
and html files
.. autoclass:: test.ReadTextFileKeywords
:members:
.. autofunction:: test.read_csv_columns_by_headers
此外,我使用的是一个虚拟环境,当我试图用命令行编译它时,它是活动的。我运行以下命令以使用sphinx编译html文件
sphinx-build -b html source build
当我运行上面的命令时,它失败了,出现以下错误
警告:autodoc:无法从模块“read_文件”导入类“ReadTextFileKeywords”;提出了以下例外情况:;没有名为pandas的模块。
如果我从pandas import pd中删除行
,然后删除函数read\u csv\u columns\u by\u headers
,并在read\u files.rst
文件中调用函数,则一切都可以正常编译。出于某种原因,sphinx似乎能够找到numpy
,但出于某种原因,sphinx似乎无法识别pandas
,尽管它们都存在于虚拟环境中,并且都加载了pip3 install
语句。有人知道为什么sphinx能够找到其他模块,但却找不到pandas
?这有什么不同?virtualenv的Lib/site packages
目录中是否有pandas
子目录?如果在virtualenv中启动Python会话并键入import
,会发生什么情况?