How to force Sphinx to use Python 3.x interpreter

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

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?

回答1:

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-disribute 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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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.



回答8:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!