This extension provides Python code intelligence on Sourcegraph. Ensure this extension is enabled and then try it on some examples:
- Tensorflow: tensorflow_ranking/examples/tf_ranking_libsvm.py
- Flask: flask/views.py
- Zulip: corporate/lib/stripe.py
- Trivial sample: pkg0/m0.py
This extension is configured to talk to a language server over WebSockets. If you are running a
private Sourcegraph instance, you should run your own language server. The server is available as a
Docker image sourcegraph/lang-python
from Docker Hub.
If you have private code, we recommend deploying the language server behind an auth proxy (such as the example below using HTTP basic authentication in NGINX), a firewall, or a VPN.
You can prevent unauthorized access to the language server by enforcing HTTP basic authentication in nginx, which comes with the sourcegraph/server image. At a high level, you'll create a secret then put it in both the nginx config and in your Sourcegraph global settings so that logged-in users are authenticated when their browser makes requests to the Python language server.
Here's how to set it up:
Create an .htpasswd
file in the Sourcegraph config directory with one entry:
$ htpasswd -c ~/.sourcegraph/config/.htpasswd langserveruser
New password:
Re-type new password:
Adding password for user langserveruser
Add a location directive the nginx.conf that will route requests to the Python language server:
...
http {
...
server {
...
location / {
...
}
location /python {
proxy_pass http://host.docker.internal:4288;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
auth_basic "basic authentication is required to access the language server";
auth_basic_user_file /etc/sourcegraph/.htpasswd;
}
}
}
- If you're running the quickstart on Linux, change
host.docker.internal
to the output ofip addr show docker0 | grep -Po 'inet \K[\d.]+'
. - If you're using Kubernetes (e.g. deploy-sourcegraph), change
host.docker.internal
tolang-python
.
Add these to your Sourcegraph global settings:
"python.serverUrl": "ws://langserveruser:PASSWORD@host.docker.internal:7080/python",
"python.sourcegraphUrl": "http://host.docker.internal:7080",
Fill in the PASSWORD
that you created above.
- If you're running the quickstart on Linux, change
host.docker.internal
to the output ofip addr show docker0 | grep -Po 'inet \K[\d.]+'
. - If you're using Kubernetes (e.g. deploy-sourcegraph):
python.serverUrl
is the address of the Python language server from the perspective of a user's browser (e.g. https://sourcegraph.example.com/python)python.sourcegraphUrl
is the address of the Sourcegraph instance from the perspective of the Python language server (e.g. http://sourcegraph-frontend:30080)
Finally, restart the sourcegraph/server container (or nginx deployment if deployed to Kubernetes) to pick up the configuration change.
After deploying the language server, unauthenticated access to http://localhost:7080/python
(or https://sourcegraph.example.com/python) should be blocked, but code intelligence should work when you're logged in.
You can always revoke the PASSWORD
by deleting the .htpasswd
file and restarting nginx.
-
Run the Python language server Docker container:
docker run --rm -p 4288:4288 sourcegraph/lang-python:insiders
-
Enable this extension on your Sourcegraph https://sourcegraph.example.com/extensions/sourcegraph/lang-python
-
Add these to your Sourcegraph settings:
"python.serverUrl": "ws://localhost:4288", "python.sourcegraphUrl": "http://host.docker.internal:7080",
If you're running on Linux, change
python.sourcegraphUrl
to the IP given by:ip addr show docker0 | grep -Po 'inet \K[\d.]+'
- Dependencies are not installed. Hovers, definitions, and references only work for sources checked into the single repository you are viewing.
- Cross-repository definitions and references are not yet supported.
- Hangs on very large repositories (e.g. django/django)