-2

I try to build the docs, and use a custom sphinx extension, but this extension isn't installed by default - so I provide it via conf.py, using this:

try:
    import sphinxjp.themes
except ImportError:
    from setuptools.command import easy_install
    easy_install.main( ["-U","sphinxjp.themes.basicstrap"] )
    import sphinxjp.themes

extensions += ['sphinxjp.themes.basicstrap']
html_theme = 'basicstrap'

html_theme_options = {
  'bootstrap_version': '3',
  'noresponsive': False,
  'inner_theme': True,
  'inner_theme_name': 'bootswatch-yeti',
}

But the problem is as follows:

  1. Python installs the extension if it is not found, or skips if it exists.
  2. But here, if it didn't exist, python will install it, and continue the execution of the file as if it didn't install it.
  3. I have then to run the file again so python can skip the installation process and build the docs.

How do I force python to load the extension just after it is installed?

I get this error:

Running Sphinx v1.3b2
Searching for sphinxjp.themes.basicstrap
Reading https://pypi.python.org/simple/sphinxjp.themes.basicstrap/
Best match: sphinxjp.themes.basicstrap 0.4.1
Downloading https://pypi.python.org/packages/source/s/sphinxjp.themes.basicstrap
/sphinxjp.themes.basicstrap-0.4.1.tar.gz#md5=bac7d878391a3dfd663b51e2311d5795
Processing sphinxjp.themes.basicstrap-0.4.1.tar.gz
Writing c:\users\abdelo~1\appdata\local\temp\easy_install-ndzj8s\sphinxjp.themes
.basicstrap-0.4.1\setup.cfg
Running sphinxjp.themes.basicstrap-0.4.1\setup.py -q bdist_egg --dist-dir c:\use
rs\abdelo~1\appdata\local\temp\easy_install-ndzj8s\sphinxjp.themes.basicstrap-0.
4.1\egg-dist-tmp-pdyuqk
Adding sphinxjp.themes.basicstrap 0.4.1 to easy-install.pth file

Installed c:\python27\lib\site-packages\sphinxjp.themes.basicstrap-0.4.1-py2.7.e
gg
Processing dependencies for sphinxjp.themes.basicstrap
Finished processing dependencies for sphinxjp.themes.basicstrap

Exception occurred:
  File "conf.py", line 11, in <module>
    import sphinxjp.themes
ImportError: No module named sphinxjp.themes
The full traceback has been saved in c:\users\abdelo~1\appdata\local\temp\sphinx
-err-wzgl0z.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message c
an be provided next time.
A bug report can be filed in the tracker at <https://bitbucket.org/birkenfeld/sp
hinx/issues/>. Thanks!

And as you can see, the extension has been installed! So I have to run it a second time so that it will work.

Abdelouahab
  • 6,473
  • 10
  • 42
  • 78

1 Answers1

1

When the extension is installed, it creates a .pth file, which would normally be processed during initialization when site.py is imported. Amongst other things, the .pth file specifies additional items that must be added to sys.path so that the new extension can be imported.

Your script therefore needs to take steps to explicitly process the .pth file after it is installed. One way to do this is to use the pkg_resources module which is part of setuptools:

try:
    import sphinxjp.themes
except ImportError:
    from pkg_resources import get_distribution
    from setuptools.command import easy_install
    easy_install.main( ["-U","sphinxjp.themes.basicstrap"] )
    get_distribution('sphinxjp.themes.basicstrap').activate()
    import sphinxjp.themes
ekhumoro
  • 98,079
  • 17
  • 183
  • 279
  • hanged on `Running Sphinx v1.3b2 Searching for sphinxjp.themes.basicstrap Reading https://pypi.python.org/simple/sphinxjp.themes.basicstrap/ Best match: sphinxjp.themes.basicstrap 0.4.1 Downloading https://pypi.python.org/packages/source/s/sphinxjp.themes.basicstrap /sphinxjp.themes.basicstrap-0.4.1.tar.gz#md5=bac7d878391a3dfd663b51e2311d5795 ` – Abdelouahab Dec 26 '14 at 19:23
  • and this will create the library on another place (used `sphinxjp.__file__`) and returns `C:\Users\\AppData\Roaming\Python\Python27\site-packages` – Abdelouahab Dec 26 '14 at 19:31
  • @Abdelouahab. I was using `--user` for testing, and it all works fine for me. Just change it back to `-U`, as in your original example. – ekhumoro Dec 26 '14 at 19:47
  • it hanged, and took so much time, and here is what it gives after `ctrl+c` : `Running Sphinx v1.3b2 Searching for sphinxjp.themes.basicstrap Reading https://pypi.python.org/simple/sphinxjp.themes.basicstrap/ Best match: sphinxjp.themes.basicstrap 0.4.1 Downloading https://pypi.python.org/packages/source/s/sphinxjp.themes.basicstrap /sphinxjp.themes.basicstrap-0.4.1.tar.gz#md5=bac7d878391a3dfd663b51e2311d5795 Configuration error: The configuration file (or one of the modules it imports) called sys.exit() ` – Abdelouahab Dec 26 '14 at 19:56
  • the idea is not only with `sphinx` but with any other python library, that requires another dependecy, so instead of using the `requirement.txt`, with this way, it will not run, because AFAIK, it must exist and runs the script again, something like `reload(module)` – Abdelouahab Dec 26 '14 at 19:59
  • @Abdelouahab. The script in my answer works perfectly fine. I've tested it both with and without the `--user` option and it downloads, installs and imports the extension with no problems. You must be doing something different from what you are showing in your question. Perhaps you need to start with a clean environment, and try again. – ekhumoro Dec 26 '14 at 20:11
  • same this, tried it on a clean env using a virtual machine, and hanged on the `/sphinxjp.themes.basicstrap-0.4.1.tar.gz#md5=bac7d878391a3dfd663b51e2311d5795 ` part – Abdelouahab Dec 26 '14 at 20:32
  • here is the file, i tried to build the docs using another sphinx theme https://github.com/abdelouahabb/tornado/blob/master/docs/conf.py – Abdelouahab Dec 26 '14 at 20:35
  • @Abdelouahab. The script is hanging for you when it attempts to download the file. Try running just these two lines: `from setuptools.command import easy_install; easy_install.main( ["-U","sphinxjp.themes.basicstrap"])`, and nothing else. If that hangs, you know for sure it is a network problem on your end. – ekhumoro Dec 26 '14 at 20:50
  • it hangs only on this command, i tried to use `pip` and it dident: `C:\Users\Abdelouahab>pip install setuptools --upgrade Downloading/unpacking setuptools from https://pypi.python.org/packages/3.4/s/set uptools/setuptools-8.4-py2.py3-none-any.whl#md5=d4e87d8d58dfd81c495d18204751ef19 Installing collected packages: setuptools Found existing installation: setuptools 7.0 Uninstalling setuptools: Successfully uninstalled setuptools Successfully installed setuptools Cleaning up... ` and even after, it hangs! – Abdelouahab Dec 26 '14 at 21:02
  • @Abdelouahab. In your original question, the ouptut shows you could download the file using easy_install without it hanging. So what have you changed since then? – ekhumoro Dec 26 '14 at 21:12
  • that is why i am surprised! thy yesterday it worked! and i dident touch anything! i even tried it on a VM that has a fresh python installation! i will accept the answer, since it worked with you, i will search about the problem with `easy_install` and why it hangs. – Abdelouahab Dec 26 '14 at 21:20
  • it seems that the bug was with the repo itself, it tried the command with some modules i dont have `easy_install.main( ["-U","webob"] )` and it works! it seems that they changed the MD5 or something about the sphinx extension! – Abdelouahab Dec 26 '14 at 21:28
  • 1
    @Abdelouahab. Well done! Glad you solved it - and thanks for the upvote/accept (assuming my answer also works for you now). – ekhumoro Dec 26 '14 at 21:33