Python-language-server: Lookup error

Created on 7 Apr 2020  路  14Comments  路  Source: microsoft/python-language-server

Environment data

  • Language Server version: 0.5.45
  • OS and version: KDE Neon 5.18
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7.15

Expected behaviour

Error getting search paths. Not sure how it affects completion.

Actual behaviour

No error.

Logs

[Info  - 4:18:22 PM] Analysis cache path: /home/cllamach/.cache/Microsoft/Python Language Server
[Info  - 4:18:22 PM] Microsoft Python Language Server version 0.5.45.0
[Info  - 4:18:22 PM] Workspace root: /home/cllamach/Panopta/classic
[Info  - 4:18:22 PM] GetCurrentSearchPaths /home/cllamach/.virtualenvs/panopta_env/bin/python 
Traceback (most recent call last):
  File "/home/cllamach/.vscode/extensions/ms-python.python-2020.3.71659/languageServer.0.5.45/get_search_paths.py", line 31, in <module>
    import site
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding
[Info  - 4:18:22 PM] Interpreter search paths:
[Info  - 4:18:22 PM]     /home/cllamach/bin/python
[Info  - 4:18:22 PM] User search paths:
[Info  - 4:18:22 PM]     /home/cllamach/Panopta/classic/src
[Info  - 4:18:23 PM] Initializing for /home/cllamach/.virtualenvs/panopta_env/bin/python
[Info  - 4:18:23 PM] Analysis caching mode: None.

Most helpful comment

yeah, seems to be an issue with the virtualenv. Calling that without it activated works, but it raised an exception when using the virtualenv python.

languageServer.0.5.45 禄 python -S -E get_search_paths.py                                                                       ~/.v/e/m/languageServer.0.5.45
/usr/lib/python2.7|stdlib|
/usr/lib/python2.7/plat-x86_64-linux-gnu|stdlib|
/usr/lib/python2.7/lib-tk|stdlib|
/usr/lib/python2.7/lib-dynload|stdlib|
/home/cllamach/.local/lib/python2.7/site-packages|pth|
/usr/local/lib/python2.7/dist-packages|site|
/usr/lib/python2.7/dist-packages|site|

languageServer.0.5.45 禄 vf activate panopta_env                                                                                ~/.v/e/m/languageServer.0.5.45

[panopta_env] languageServer.0.5.45 禄 python -S -E get_search_paths.py                                                         ~/.v/e/m/languageServer.0.5.45
Traceback (most recent call last):
  File "get_search_paths.py", line 31, in <module>
    import site
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

Creating any new virtualenv triggers the same error.

I was able to solve the issue by creating the virtualenv directly with the virtualenv command, rather than using a fish shell extension to handle them. Not sure why it breaks it. I'll close the issue, thanks for your help.

All 14 comments

Some quick searching shows that this message is an issue with the Python install. It shouldn't fail to find the UTF-8 encoder (that would be pretty catastrophic). Given this is a virtual environment, did you update the version of Python this environment is based on?

I'm also seeing people mention that it can occur when there's a module in the path called "unicode", i.e. you've named a module the same as a builtin.

No, this just happened on a freshly installed machine of KDE Neon 5.18, system python, 2.7.17 (sorry, thought it was .15). The base for neon should be Ubuntu 18.04, so not sure how that would mess up the python installation.

Other weird thing is that if I open an ipython shell from that virtualenv, encoding a string in utf-8 doesn't raise an exception.

[panopta_env] infrastructure [master]脳 禄 ipython                                                                                           ~/P/infrastructure
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: p = '/home/cllamach'

In [2]: p.encode('utf-8')
Out[2]: '/home/cllamach'

In [3]: 

Is there a way I could replicate that get_search_paths.py call from outside the language server?

Yes, the file exists in the language server's download folder. It should be somewhere in $HOME/.vscode/extensions/ms-python-*/langaugeServer (a path like this, from memory), then runnable just as a script within the environment like:

$ python -S -E /path/to/get_search_paths.py

yeah, seems to be an issue with the virtualenv. Calling that without it activated works, but it raised an exception when using the virtualenv python.

languageServer.0.5.45 禄 python -S -E get_search_paths.py                                                                       ~/.v/e/m/languageServer.0.5.45
/usr/lib/python2.7|stdlib|
/usr/lib/python2.7/plat-x86_64-linux-gnu|stdlib|
/usr/lib/python2.7/lib-tk|stdlib|
/usr/lib/python2.7/lib-dynload|stdlib|
/home/cllamach/.local/lib/python2.7/site-packages|pth|
/usr/local/lib/python2.7/dist-packages|site|
/usr/lib/python2.7/dist-packages|site|

languageServer.0.5.45 禄 vf activate panopta_env                                                                                ~/.v/e/m/languageServer.0.5.45

[panopta_env] languageServer.0.5.45 禄 python -S -E get_search_paths.py                                                         ~/.v/e/m/languageServer.0.5.45
Traceback (most recent call last):
  File "get_search_paths.py", line 31, in <module>
    import site
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/cllamach/.virtualenvs/panopta_env/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

Creating any new virtualenv triggers the same error.

I was able to solve the issue by creating the virtualenv directly with the virtualenv command, rather than using a fish shell extension to handle them. Not sure why it breaks it. I'll close the issue, thanks for your help.

Thank you for the update!

I'm currently hitting this after a fresh ubuntu install (18.04) with python2.7 as well, creating the virtualenv manually yields the same error.

[Info  - 3:19:34 a.m.] Analysis cache path: /home/jordan/.cache/Microsoft/Python Language Server
[Info  - 3:19:34 a.m.] Microsoft Python Language Server version 0.5.50.0
[Info  - 3:19:34 a.m.] Workspace root: /home/jordan/dev/reponame
[Info  - 3:19:34 a.m.] GetCurrentSearchPaths /home/jordan/Envs/reponame/bin/python
Traceback (most recent call last):
  File "/home/jordan/.config/coc/extensions/coc-python-data/languageServer.0.5.50/get_search_paths.py", line 31, in <module>
    import site
  File "/home/jordan/Envs/reponame/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/jordan/Envs/reponame/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/jordan/Envs/reponame/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

Any suggestions?

What happens if you call this python -S -E /home/jordan/.config/coc/extensions/coc-python-data/languageServer.0.5.50/get_search_paths.py while the virtualenv is disabled completely? That's how I figured out that it was my virtualenv that had the issue.

It seems to be specifically related to a python2 virtualenv. Running that command using global py2 works fine. I've tried recreating it with virtualenv manually and using virtualenvwrapper but no luck.

Anyway, appreciate the response @CMLL

Did the virtualenv came installed with python? Can you try installing it new? I believe it can be installed from pip.

I manually installed virtualenv and virtualenvwrapper globally with pip2

I'm having trouble also. I use pipenv in my project:

[Info  - 8:13:29 p.m.] Analysis caching mode: None.
Traceback (most recent call last):
  File "/home/alan/.vscode/extensions/ms-python.python-2020.6.80635-dev/languageServer.0.5.50/get_search_paths.py", line 31, in <module>
    import site
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

I can repro with pipenv run:

$ pipenv run python -S -E /home/alan/.vscode/extensions/ms-python.python-2020.6.80635-dev/languageServer.0.5.50/get_search_paths.py
Traceback (most recent call last):
  File "/home/alan/.vscode/extensions/ms-python.python-2020.6.80635-dev/languageServer.0.5.50/get_search_paths.py", line 31, in <module>
    import site
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

or just trying to import site;site.main() from pipenv run:

$ pipenv run python -S -E -c "import site;site.main()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 165, in <module>
    main()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 19, in main
    load_host_site()
  File "/home/alan/.local/share/virtualenvs/project-GnuKVr7R/lib/python2.7/site.py", line 54, in load_host_site
    full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
LookupError: no codec search functions registered: can't find encoding

However, dropping the -S flag makes it work:

$ pipenv run python -E -c "import site;site.main()"
$ pipenv run python -E -c "import encodings;print encodings"
<module 'encodings' from '/usr/lib/python2.7/encodings/__init__.pyc'>

...except for the language server, which demands -S:

$pipenv run python -E /home/alan/.vscode/extensions/ms-python.python-2020.6.80635-dev/languageServer.0.5.50/get_search_paths.py
Traceback (most recent call last):
  File "/home/alan/.vscode/extensions/ms-python.python-2020.6.80635-dev/languageServer.0.5.50/get_search_paths.py", line 27, in <module>
    raise RuntimeError('script must be run with -S')
RuntimeError: script must be run with -S

Any idea what I'm doing wrong?

I run into the same issue as @darkvertex, using pipenv with a Python 2.7.17 interpreter. This is what I found:

  • Running python2 directly from outside the virtualenv created by pipenv works file:
$ python2 -S -E -c "print('hi'.encode('utf-8'))"
hi
  • Running the same, but using the interpreter inside the virtualenv created by pipenv fails with the error I see on the language server console in vscode:
$ pipenv run python -S -E -c "print('hi'.encode('utf-8'))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
LookupError: no codec search functions registered: can't find encoding

There's clearly something wrong with the virtualenv, but can't yet figure out what it is.

I'm experiencing the same problems.

@symc-gail - Try Pylance language server on the VS Code marketplace. This server is no longer actively developed.

Was this page helpful?
0 / 5 - 0 ratings