0

I've created a virtual environment using both virtualenv and pipenv and in the both cases it seems that sphinx is not able to figure out the correct Python version. I have installed Python 2.7 and Python 3.8 in my global environment.

The error shows up when I try to use sphinx-apidoc + make html. I'm on a Windows 10 machine. Because I'm using type annotations, I get this error:

(venv) C:\Users\eug\Documents\learning\learning-pdoc\docs>make html
Running Sphinx v1.8.5
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 3 source files that are out of date
updating environment: 3 added, 0 changed, 0 removed
reading sources... [100%] sample_package
WARNING: autodoc: failed to import module u'core' from module u'sample_package'; the following exception was raised:
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\sphinx\ext\autodoc\importer.py", line 154, in import_module
    __import__(modname)
  File "C:\Users\eug\Documents\learning\learning-pdoc\sample_package\core.py", line 4
    def sample_function2(a : Number, b : Number)->Number:
                           ^
SyntaxError: invalid syntax

As you can see I'm currently on the virtual environment (venv). Calling python by itself correctly calls the right version. In order to execute what I want I need to call:

python -m sphinx.cmd.build -M html . .

Which is not ideal. Is there a way to fix this?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Eug_Zazou
  • 155
  • 1
  • 11
  • Here's what probably went wrong, you installed Sphinx to the Python2 base installation (not even the venv) look at the line "File "c:\python27\lib\...". When creating the venv you should have run explicitly Python3 (instead of letting the environment variables decide for you). Afterwards with the venv created, you should have activated the Python3 venv to install Sphinx specifically to it. And finally before you run Sphinx wtih `make html`, you should also activate the Python 3 venv. – bad_coder Feb 19 '21 at 11:36
  • I don't have sphinx installed on the Python2 base installation. Also I always create the virtual environment with the desired Python version by using `--python` keyword. I can confirm the Python is the good version when inside the environment, because calling it does come with right version. I also activate the Python environment, as you can see on the output I published (there is (venv) in front of the text). – Eug_Zazou Feb 22 '21 at 10:02
  • You are using Sphinx v1.8.5, that's the last version to support Python 2 syntax. See [deprecated 2.0.0b1](https://www.sphinx-doc.org/en/master/changes.html?highlight=release%20notes#id81). Problem now is, type anotations are only Python 3.5 onward, see [PEP 484](https://www.python.org/dev/peps/pep-0484/). If you are trying to document Python 3 code use the latext Sphinx version, if you are trying to document Python 2 use Sphinx v1.8.5. Otherwise you are missing hundreds of fixes released in the meanwhile. – bad_coder Feb 22 '21 at 14:20
  • The best way to do this is having 2 separate venvs, one for Python 2 another for Python 3. With different Sphinx versions installed on each one. Otherwise you'll be running into an endless number of bugs and incompatibilities. But it's hard to answer further without more specific information about how your environment is configured. – bad_coder Feb 22 '21 at 14:24
  • If you ask me this line `"c:\python27\lib\site-packages\sphinx\ext\autodoc\importer.py"` are the site packages of the Python 2 base installation, so it does seem Sphinx is installed there. – bad_coder Feb 22 '21 at 14:31
  • I get what you mean, but I configured the environment in Python 3 and used both `pip install` (for the one created with `virtualenv`) and `pipenv install`(created with `pipenv`). They are separate environments created respectivelly as `python3_64 -m virtualenv venv` and `pipenv --python python3_64 shell`. I'm not mixing versions. If I look into my pipfile for example, it says that the version of python is 3.8 – Eug_Zazou Feb 22 '21 at 15:11
  • In this case I can't answer, I'm not experienced enough with `virtualenv` to understand how the different environments are being resolved. But it's an interesting question, I would advise editing the question to add the `virtualenv` tag since the problem seems specific to it. If you haven't seen it already [this thread](https://stackoverflow.com/a/41573588) gives a good overview. – bad_coder Feb 22 '21 at 15:33

0 Answers0