API Overview

Note: This documentation is for Plugin developers, who want to improve their editors/IDE autocompletion

If you want to use Jedi, you first need to import jedi. You then have direct access to the Script. You can then call the functions documented here. These functions return API classes.

Deprecations

The deprecation process is as follows:

  1. A deprecation is announced in the next major/minor release.
  2. We wait either at least a year & at least two minor releases until we remove the deprecated functionality.

API Documentation

The API consists of a few different parts:

Static Analysis Interface

Jedi is a static analysis tool for Python that can be used in IDEs/editors. Its historic focus is autocompletion, but does static analysis for now as well. Jedi is fast and is very well tested. It understands Python on a deeper level than all other static analysis frameworks for Python.

Jedi has support for two different goto functions. It’s possible to search for related names and to list all names in a Python file and infer them. Jedi understands docstrings and you can use Jedi autocompletion in your REPL as well.

Jedi uses a very simple API to connect with IDE’s. There’s a reference implementation as a VIM-Plugin, which uses Jedi’s autocompletion. We encourage you to use Jedi in your IDEs. It’s really easy.

To give you a simple example how you can use the Jedi library, here is an example for the autocompletion feature:

>>> import jedi
>>> source = '''
... import datetime
... datetime.da'''
>>> script = jedi.Script(source, 3, len('datetime.da'), 'example.py')
>>> script
<Script: 'example.py' ...>
>>> completions = script.completions()
>>> completions                                         
[<Completion: date>, <Completion: datetime>, ...]
>>> print(completions[0].complete)
te
>>> print(completions[0].name)
date

As you see Jedi is pretty simple and allows you to concentrate on writing a good text editor, while still having very good IDE features for Python.

class jedi.Script(source=None, line=None, column=None, path=None, encoding='utf-8', sys_path=None, environment=None)[source]

A Script is the base for completions, goto or whatever you want to do with Jedi.

You can either use the source parameter or path to read a file. Usually you’re going to want to use both of them (in an editor).

The script might be analyzed in a different sys.path than Jedi:

  • if sys_path parameter is not None, it will be used as sys.path for the script;
  • if sys_path parameter is None and VIRTUAL_ENV environment variable is defined, sys.path for the specified environment will be guessed (see jedi.evaluate.sys_path.get_venv_path()) and used for the script;
  • otherwise sys.path will match that of Jedi.
Parameters:
  • source (str) – The source code of the current file, separated by newlines.
  • line (int) – The line to perform actions on (starting with 1).
  • column (int) – The column of the cursor (starting with 0).
  • path (str or None) – The path of the file in the file system, or '' if it hasn’t been saved yet.
  • encoding (str) – The encoding of source, if it is not a unicode object (default 'utf-8').
  • sys_path (Environment) – sys.path to use during analysis of the script
  • environment – TODO
completions()[source]

Return classes.Completion objects. Those objects contain information about the completions, more than just names.

Returns:Completion objects, sorted by name and __ comes last.
Return type:list of classes.Completion
goto_definitions()[source]

Return the definitions of a the path under the cursor. goto function! This follows complicated paths and returns the end, not the first definition. The big difference between goto_assignments() and goto_definitions() is that goto_assignments() doesn’t follow imports and statements. Multiple objects may be returned, because Python itself is a dynamic language, which means depending on an option you can have two different versions of a function.

Return type:list of classes.Definition
goto_assignments(follow_imports=False, follow_builtin_imports=False)[source]

Return the first definition found, while optionally following imports. Multiple objects may be returned, because Python itself is a dynamic language, which means depending on an option you can have two different versions of a function.

Parameters:
  • follow_imports – The goto call will follow imports.
  • follow_builtin_imports – If follow_imports is True will decide if it follow builtin imports.
Return type:

list of classes.Definition

usages(additional_module_paths=(), **kwargs)[source]

Return classes.Definition objects, which contain all names that point to the definition of the name under the cursor. This is very useful for refactoring (renaming), or to show all usages of a variable.

Parameters:
  • additional_module_paths – Deprecated, never ever worked.
  • include_builtins – Default True, checks if a usage is a builtin (e.g. sys) and in that case does not return it.
Return type:

list of classes.Definition

call_signatures()[source]

Return the function object of the call you’re currently in.

E.g. if the cursor is here:

abs(# <-- cursor is here

This would return the abs function. On the other hand:

abs()# <-- cursor is here

This would return an empty list..

Return type:list of classes.CallSignature
class jedi.Interpreter(source, namespaces, **kwds)[source]

Jedi API for Python REPLs.

In addition to completion of simple attribute access, Jedi supports code completion based on static code analysis. Jedi can complete attributes of object which is not initialized yet.

>>> from os.path import join
>>> namespace = locals()
>>> script = Interpreter('join("").up', [namespace])
>>> print(script.completions()[0].name)
upper

Parse source and mixin interpreted Python objects from namespaces.

Parameters:
  • source (str) – Code to parse.
  • namespaces (list of dict) – a list of namespace dictionaries such as the one returned by locals().

Other optional arguments are same as the ones for Script. If line and column are None, they are assumed be at the end of source.

jedi.names(source=None, path=None, encoding='utf-8', all_scopes=False, definitions=True, references=False, environment=None)[source]

Returns a list of Definition objects, containing name parts. This means you can call Definition.goto_assignments() and get the reference of a name. The parameters are the same as in Script, except or the following ones:

Parameters:
  • all_scopes – If True lists the names of all scopes instead of only the module namespace.
  • definitions – If True lists the names that have been defined by a class, function or a statement (a = b returns a).
  • references – If True lists all the names that are not listed by definitions=True. E.g. a = b returns b.
jedi.preload_module(*modules)[source]

Preloading modules tells Jedi to load a module now, instead of lazy parsing of modules. Usful for IDEs, to control which modules to load on startup.

Parameters:modules – different module names, list of string.
jedi.set_debug_function(func_cb=<function print_to_stdout>, warnings=True, notices=True, speed=True)[source]

Define a callback debug function to get all the debug messages.

If you don’t specify any arguments, debug messages will be printed to stdout.

Parameters:func_cb – The callback function for debug messages, with n params.

Environments

Environments are a way to activate different Python versions or Virtualenvs for static analysis. The Python binary in that environment is going to be executed.

jedi.find_system_environments()[source]

Ignores virtualenvs and returns the Python versions that were installed on your system. This might return nothing, if you’re running Python e.g. from a portable version.

The environments are sorted from latest to oldest Python version.

Yields:Environment
jedi.find_virtualenvs(paths=None, **kwargs)[source]
Parameters:
  • paths – A list of paths in your file system to be scanned for Virtualenvs. It will search in these paths and potentially execute the Python binaries. Also the VIRTUAL_ENV variable will be checked if it contains a valid Virtualenv.
  • safe – Default True. In case this is False, it will allow this function to execute potential python environments. An attacker might be able to drop an executable in a path this function is searching by default. If the executable has not been installed by root, it will not be executed.
Yields:

Environment

jedi.get_system_environment(version)[source]

Return the first Python environment found for a string of the form ‘X.Y’ where X and Y are the major and minor versions of Python.

Raises:InvalidPythonEnvironment
Returns:Environment
jedi.create_environment(path, safe=True)[source]

Make it possible to manually create an Environment object by specifying a Virtualenv path or an executable path.

Raises:InvalidPythonEnvironment
Returns:Environment
jedi.get_default_environment()[source]

Tries to return an active Virtualenv. If there is no VIRTUAL_ENV variable set it will return the latest Python version installed on the system. This makes it possible to use as many new Python features as possible when using autocompletion and other functionality.

Returns:Environment
exception jedi.InvalidPythonEnvironment[source]

If you see this exception, the Python executable or Virtualenv you have been trying to use is probably not a correct Python version.

class jedi.api.environment.Environment(executable)[source]

This class is supposed to be created by internal Jedi architecture. You should not create it directly. Please use create_environment or the other functions instead. It is then returned by that function.

get_sys_path(*args, **kwargs)[source]

The sys path for this environment. Does not include potential modifications like sys.path.append.

Returns:list of str

Examples

Completions:

>>> import jedi
>>> source = '''import json; json.l'''
>>> script = jedi.Script(source, 1, 19, '')
>>> script
<jedi.api.Script object at 0x2121b10>
>>> completions = script.completions()
>>> completions
[<Completion: load>, <Completion: loads>]
>>> completions[1]
<Completion: loads>
>>> completions[1].complete
'oads'
>>> completions[1].name
'loads'

Definitions / Goto:

>>> import jedi
>>> source = '''def my_func():
...     print 'called'
...
... alias = my_func
... my_list = [1, None, alias]
... inception = my_list[2]
...
... inception()'''
>>> script = jedi.Script(source, 8, 1, '')
>>>
>>> script.goto_assignments()
[<Definition inception=my_list[2]>]
>>>
>>> script.goto_definitions()
[<Definition def my_func>]

Related names:

>>> import jedi
>>> source = '''x = 3
... if 1 == 2:
...     x = 4
... else:
...     del x'''
>>> script = jedi.Script(source, 5, 8, '')
>>> rns = script.related_names()
>>> rns
[<RelatedName x@3,4>, <RelatedName x@1,0>]
>>> rns[0].start_pos
(3, 4)
>>> rns[0].is_keyword
False
>>> rns[0].text
'x'