如何强制Sphinx使用Python 3.x解释器

发布于 2024-12-14 01:50:16 字数 577 浏览 4 评论 0原文

我尝试为使用 Python 3.x 编写的项目创建文档。 Sphinx 是我想要使用的工具,并且根据< a href="http://sphinx.pocoo.org/changes.html" rel="noreferrer">官方网站,最新版本1.1.2兼容Python 3.1+。我的操作系统是 Archlinux,一个使用 Python 3.2+ 作为默认 Python 包的 Linux 发行版。

安装和配置非常简单(easy_install -U Sphinx 然后 sphinx-quickinstall),并且从未要求我在 2.x 或 3.x Python 解释器之间进行选择。但是当我要求 Sphinx 创建我的项目的文档时,我的代码会被分析,就像我为 Python 2.x 编写的一样。

Sphinx 准备好支持 Python 3.x 了吗?我做错了吗?

I try to create documentation for a project written with Python 3.x. Sphinx is the tool I want to use and, according to the official site, its last version 1.1.2 is compatible with Python 3.1+. My OS is Archlinux, a Linux distribution which uses Python 3.2+ as the default Python package.

The installation and configuration is straight forward (easy_install -U Sphinx then sphinx-quickinstall) and I was never asked to choose between the 2.x or 3.x Python interpreter. But when I ask Sphinx to create the documentation of my project, my code is analyzed as if I wrote it for Python 2.x.

Is Sphinx ready for Python 3.x? Did I make a mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

秋心╮凉 2024-12-21 01:50:16

安装:使用pippython3安装sphinx(pip3就像这样)。

    pip3 install -U sphinx

构建: Makefile(linux/Mac) 更改。

    SPHINXBUILD   = python -msphinx

在 Makefile 的上面一行中,将 python 更改为 python3(或 python3.x),就像

   SPHINXBUILD   = python3 -msphinx

默认 python 指向 2.x 版本一样
Python。

Installation: Install sphinx with pip for python3(pip3 like that).

    pip3 install -U sphinx

Building: Makefile(linux/Mac) changes.

    SPHINXBUILD   = python -msphinx

In above line in Makefile change python to python3(or python3.x) like

   SPHINXBUILD   = python3 -msphinx

if default python is pointing to 2.x version
python.

屋檐 2024-12-21 01:50:16

在 Ubuntu 上,python3-sphinx 是一个单独的包。就我而言,我需要安装 python3-sphinx:

sudo apt-get install python3-sphinx

您可能可以在一台机器上运行两者,但我只是删除了旧的:

sudo apt-get remove python-sphinx

在此之后,我的旧 makefile 与我的 Python 3 代码一起工作得很好。

On Ubuntu, python3-sphinx is a separate package. In my case, I needed to install python3-sphinx:

sudo apt-get install python3-sphinx

You can probably run both on a machine, but I just removed the old one:

sudo apt-get remove python-sphinx

My old makefile worked just fine with my Python 3 code after this.

江南烟雨〆相思醉 2024-12-21 01:50:16

我来自未来(2020)。

我在让 Sphinx 与特定的 python 解释器和 Ubuntu 完美配合时遇到了问题。只是认为最新的答案可能会帮助其他在这里徘徊的人。

===================================

所以,首先要了解的是,如果您使用自动构建/安装选项,您仍然应该仔细检查您是否得到了您认为应该得到的东西。

例如,运行(按照 Sphinx 文档 顶部的建议)

apt install python3-sphinx

,然后在要构建自动文档的目录中运行 sphinx-quickstart 即可。但是,它将使用 Sphinx 1.6.7(在撰写本文时,最新的 Sphinx 版本是 3.0.3),因为该存储库显然没有维护。然后,如果您想使用一些 Sphinx 扩展(例如 sphinx-js,这就是我的情况),那么您可能会感到有点惊讶。

因此,我建议仅将这些自动打包安装用于简单的用例或测试或原型设计。

===========================================

更好的方法

使用 pip 安装 SPHINX(Py3 为 pip3)

pip3 install -U sphinx

现在您拥有最新的 pip3 版本,并且它正在与特定的已知解释器一起使用。您可以使用pip3 list仔细检查

初始化项目的autodoc

如果执行上述操作,sphinx-quickstart可能无法工作:找不到命令。我猜测 apt install 负责将一些脚本(例如 sphinx-quickstart)放入路径中。

但是您可以这样做:

python3 -m sphinx.cmd.quickstart 

在文档目录的根目录中。这将非常类似于 sphinx-quickstart。如果您想要更多功能,您可能需要向新创建的conf.py添加扩展。例如(有关详细信息,请参阅 Sphinx 的文档):

extensions = ['sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.viewcode',
    'sphinx.ext.githubpages']

构建文档

最后,make html 如果以这种方式安装,则无法开箱即用。但是,您可以改为启动完整命令:

[pythonterpreter] -m sphinx.cmd.build -b [builder type] [path-to-conf.py] [path-output-directoy]

例如:

python3 -m sphinx.cmd.build -b html /home/anonymous/PycharmProjects/DocstringDemo/docstrings /home/anonymous/PycharmProjects/DocstringDemo/docstrings/_build

这很漂亮与 makefile 本身的作用差不多。

现在你已经安装了带有特定解释​​器的 Sphinx

I'm from the future (2020).

I've had issues making Sphinx play nicely with a specific python interpreter and Ubuntu. Just thought an up to date answer might help others wandering here.

===================================

So, the first thing to understand is that if you use the automated build/installation option, you should still double-check that you're getting what you think you should.

For instance, running (as suggested at the top of Sphinx's doc)

apt install python3-sphinx

and then running sphinx-quickstart in a directory where you want to build the auto-doc will work. However, it will use Sphinx 1.6.7 (at the time of writing the latest Sphinx version is 3.0.3), since the repo is apparently not maintained. Then if you want to use some Sphinx extensions (like sphinx-js, which was my case) then you may be in for a bit of a surprise.

So I would recommend using those automated packaged install only for straightforward use cases or testing or prototyping.

=========================================

A better approach

Install SPHINX using pip (pip3 for Py3)

pip3 install -U sphinx

Now you have the lastest pip3 release and it is working with a specific, known interpreter. You can double-check with pip3 list

Init you project's autodoc

If doing the above, sphinx-quickstart may not work: command not found. I am guessing that the apt install takes care of putting some scripts, such as sphinx-quickstart, in the path.

However you can do:

python3 -m sphinx.cmd.quickstart 

in the root of your documentation's directory. This will act much like sphinx-quickstart. If you want the more features, you may need to add extensions to the newly created conf.py. For exemple (see Sphinx's doc for details):

extensions = ['sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.viewcode',
    'sphinx.ext.githubpages']

Building your doc

Then finally, make html if installed this way will not work out of the box. However, you can instead launch the full command instead:

[python interpreter] -m sphinx.cmd.build -b [builder type] [path-to-conf.py] [path-output-directoy]

For exemple:

python3 -m sphinx.cmd.build -b html /home/anonymous/PycharmProjects/DocstringDemo/docstrings /home/anonymous/PycharmProjects/DocstringDemo/docstrings/_build

Which is pretty much what the makefile itself does.

So now you have Sphinx installed with a specific interpreter

顾冷 2024-12-21 01:50:16

昨晚,当我遇到你的问题时,我也遇到了同样的问题。 — 我也在 Arch 上。

我想问题可能有很多不同的原因,但对我来说,解决方案是我安装了 Python 2 版本的 python-distribute 软件包,因此安装了 easy_install-2.7而不是easy_install-3.2

我相信在我的情况下,我之前尝试从 pacman 安装 Sphinx(安装版本 1.0.8)时安装了错误版本的 python-distribute,因此卸载 Sphinx 以及随后所有不需要的依赖项 pacman -Rsu python-sphinx 然后安装 python-distribute 得到了正确版本的 easy_install,然后重新安装 Sphinx easy_install 和 Sphinx 安装按预期工作。

如果您还有其他依赖于python-distribute的东西,那么过程可能会有点不同。但首先尝试删除 python-distribute 并从那里开始工作。

废弃最后一部分。时间太早了,我还没有好好思考! python2-distributepython-distribute 是单独的包,我相信它们可以共存。因此,如果这是您的问题,您需要做的就是检查您是否有 python-distribute (不是“2”),如果没有安装它,然后确保您使用 easy_install-3.2< /code> 安装 Sphinx。

希望这对您有帮助。

I had this exact same problem last night, when I came across your question. — I am also on Arch.

I guess the problem could be a number of different things, but the solution for me was that I had the Python 2 version of the python-distribute package installed and therefore had easy_install-2.7 not easy_install-3.2.

I believe in my case the wrong version of python-distribute was installed by my attempt to previously install Sphinx from pacman (which installs version 1.0.8), so uninstalling Sphinx and all subsequently unneeded dependencies pacman -Rsu python-sphinx and then installing python-distribute got me the right version of easy_install, then reinstalling Sphinx with easy_install and the Sphinx installation works as expected.

If you have other things that depend on python-distribute then the process may be a little different. But start by just trying to remove python-distribute and work from there.

Scrap that last part. It's too early in the morning and I wasn't thinking straight! python2-distribute and python-distribute are seperate packages which I believe can co-exist. So, if this is your problem all you need to do is check you have python-distribute (not "2"), if not install it, and then ensure you use easy_install-3.2 to install Sphinx.

Hope this helps you.

猫七 2024-12-21 01:50:16

我在 Ubunut 也遇到了同样的问题。我不会使用 Josh_PNicolas 答案,因为..

  • 我不想更改我的 PYTHON 路径。
  • 我不想卸载 python-sphinx,因为我需要它与旧项目一起使用。

所以我用这个名为 sphinx3-build 的小脚本修复了它:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Same as /usr/bin/sphinx-build but with different
interpreter
"""

import sys

if __name__ == '__main__':
    from sphinx import main, make_main
    if sys.argv[1:2] == ['-M']:
        sys.exit(make_main(sys.argv))
    else:
        sys.exit(main(sys.argv))

它与 sphinx-build 相同,但使用不同的解释器。
在 Makefile 中,我更改了以下行:

SPHINXBUILD   = sphinx3-build

现在将文件复制到 /usr/bin/ 文件夹:

sudo cp sphinx3-build /usr/bin/

这对我有用。您也可以在本地将其用于一个项目,只需将
将脚本放入与 Makefile 相同的文件夹中,并将 SPHINXBUILD 设置为
./sphinx3-build

I'm on Ubunut and had the same problem. I won't use Josh_P or Nicolas answer because..

  • I don't want to change my PYTHON path.
  • I don't want to uninstall python-sphinx because I need it with older Projects.

So i fixed it with this little script called sphinx3-build:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Same as /usr/bin/sphinx-build but with different
interpreter
"""

import sys

if __name__ == '__main__':
    from sphinx import main, make_main
    if sys.argv[1:2] == ['-M']:
        sys.exit(make_main(sys.argv))
    else:
        sys.exit(main(sys.argv))

It's the same as sphinx-build but with a different interpreter.
In the Makefile I changed the following line:

SPHINXBUILD   = sphinx3-build

Now copy the file to the /usr/bin/ folder:

sudo cp sphinx3-build /usr/bin/

This worked for me. You could also use it locally for one project only by placing the
script into the same folder as the Makefile and set SPHINXBUILD to
./sphinx3-build.

蓝海似她心 2024-12-21 01:50:16

与已经提供的解决方案类似的解决方案是将对 sphinx.main() 的调用直接放入 Makefile 中的 SPHINXBUILD 变量中:

SPHINXBUILD   = python3 -c "import sys,sphinx;sys.exit(sphinx.main(sys.argv))"

sphinx 生成的“用户友好检查 sphinx-build”代码块失败,所以我只是将其删除它。这个解决方案对我来说更可取,因为它不需要单独的脚本,也不需要删除任何 python 安装或 sphinx 模块。

A similar solution to those already offered is to put the call to sphinx.main() right into the SPHINXBUILD variable in the Makefile:

SPHINXBUILD   = python3 -c "import sys,sphinx;sys.exit(sphinx.main(sys.argv))"

The sphinx-generated "User-friendly check for sphinx-build" block of code fails then so I just removed it. This solution was preferable to me since it didn't require a separate script nor the removal of any python installation or sphinx module.

不一样的天空 2024-12-21 01:50:16

看来 Sphinx 的安装仅支持 Python-2。尽管有多种方法可以为 python3 安装 Sphinx,但只需使用 virtualenv 创建一个使用默认为python3。

virtualenv -p /path/to/python-3 foo

在 vi​​rtualenv 中安装 Sphinx:

pip install Sphinx

作为奖励,这种方法允许您为不同的环境创建自定义环境项目。

附言。您可能需要考虑使用 virtualenvwrapper

It seems that Sphinx is installed only with Python-2 support. Although there are various ways to install Sphinx for python3, just use virtualenv to create a custom environment that uses python3 by default.

virtualenv -p /path/to/python-3 foo

And inside the virtualenv install Sphinx:

pip install Sphinx

As a bonus, this approach allows you to create custom environments for different projects.

PS. You might want to consider using virtualenvwrapper.

我们只是彼此的过ke 2024-12-21 01:50:16

当我寻找答案时,这个网站一遍又一遍地出现。我认为答案并不容易找到,因为其他人都比我更了解狮身人面像。但如果有人仍在寻找答案,这就是我最终得到的结果:

sudo apt-get update

sudo apt-get install python3

sudo apt-get install sqlite3

sudo apt-get installidle3

sudo apt-get install python3-pip

sudo apt-get 安装 python3-docutils

sudo apt-get 安装 python3-jinja2

sudo apt-get 安装 python3-pygments

sudo pip3 install Sphinx

关键是我错过了有一个 pip3 的事实。我也习惯在添加包之前更新系统。

When I searched for an answer, this is the site that came up over and over. I'm thinking the answer is not easy to find because everyone else understands sphinx better than I can figure out. But if any is still searching for an answer, this is what eventually I ended up with:

sudo apt-get update

sudo apt-get install python3

sudo apt-get install sqlite3

sudo apt-get install idle3

sudo apt-get install python3-pip

sudo apt-get install python3-docutils

sudo apt-get install python3-jinja2

sudo apt-get install python3-pygments

sudo pip3 install Sphinx

The key was I was missing the fact that there was a pip3. I also update the system before adding a package as habit.

临走之时 2024-12-21 01:50:16

我认为 Sphinx 只是使用“python”命令作为解释器。但根据 Makefile,您可以在运行 make 进行安装时使用 PYTHON 选项指定您自己的文件。

I think Sphinx simply uses the "python" command as interpreter. But according to the Makefile, you can specify your own with the PYTHON option, when running make for installation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文