Pants will load a REPL with all of your specified source code and any of its third-party dependencies, which allows you to import those values.
In addition to the default Python shell, Pants supports the improved IPython shell.
To use IPython, run
./pants repl --shell=ipython. To permanently use IPython, add this to your
[repl] shell = "ipython"
You can change IPython's version with
[ipython].version. If you change it, Pants's default lockfile for IPython will not work. Either set the
lockfile option to a custom path or
"<none>" to opt-out. See Third-party dependencies.
[ipython] version = "ipython>=8.0.0" lockfile = "3rdparty/python/ipython_lock.txt"
If you set the
version lower than IPython 7, then you must set
[ipython].ignore_cwd = false to avoid Pants setting an option that did not exist in earlier IPython releases.
Python 2 support
Pants uses IPython 7 by default, which does not work with Python 2. You can override
versionto use IPython 5. As mentioned above, you must set
ignore_cwd = false.
[ipython] version = "ipython<6" lockfile = "3rdparty/python/ipython_lock.txt" ignore_cwd = false
You can even use IPython 7 for Python 3 code, and IPython 5 for Python 2 code:
[ipython] version = "ipython==7.16.1 ; python_version >= '3.6'" extra_requirements.add = ["ipython<6 ; python_version == '2.7'"] lockfile = "3rdparty/python/ipython_lock.txt" ignore_cwd = false
$ ./pants repl helloworld/greet/greeting.py Python 3.7.6 (default, Feb 26 2020, 08:28:08) [Clang 11.0.0 (clang-1184.108.40.206)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from helloworld.greet.greeting import Greeter >>> Greeter().greet("Pants") 'buenas tardes, Pants!' >>> from translate import Translator >>> Translator(to_lang="fr").translate("Good morning.") 'Salut.'
This will not load any of your code:
❯ ./pants repl --shell=ipython Python 3.9.12 (main, Mar 26 2022, 15:45:34) Type 'copyright', 'credits' or 'license' for more information IPython 7.34.0 -- An enhanced Interactive Python. Type '?' for help. In : 21 * 4 Out: 84
./pants repl :: will load all your code.
Tip: how to exit the REPL
exit()and hit enter, or press
Updated 3 months ago