Skip to content

External Command: python3

Terry Chou edited this page Dec 29, 2019 · 2 revisions

python3

Pre-set related environment variables

The following environment variables are set every time iVim launches:

  1. python: PYTHONHOME, PYTHONIOENCODING, PYTHONPATH

  2. pip: PIP_DISABLE_PIP_VERSION_CHECK, PIP_NO_COLOR, PIP_TARGET

Install package via pip

iVim has the Python package installer pip pre-installed. You can use it to install packages as you do on other platforms.

One note though: only packages written in pure python are supported. Others may fail or not work properly.

Run modules

You can always run built-in or installed modules with the -m option of python:

:!python3 -m http.server

this starts the simple http server.

Run python scripts directly as commands

If you want to run a script directly like a command:

:!myscript.py

usually, this returns a disappointing message: myscript.py: command not found.

To make this work, you need to put the script into the search path, and make it executable.

Steps in details

The following are the steps of how you can do this in iVim:

1. Put the script into one of the directories in $PATH

For the system to locate it, the script needs to be in one of the directories set in $PATH. You can meet this requirement in two ways:

a) move the script into one of the existing directories

To list the directories:

:echo $PATH

then pick one as the target, do the moving:

:!mv myscript.py targetdir/

b) or just add the directory to $PATH

To add your directory to the environment variable:

:!setenv PATH $PATH:targetdir

this appends your directory at the end of the search paths.

2. Make the script executable

Add the shebang to the first line of the script, so that the system can recognize it:

#!/usr/bin/env python3

Also the script needs to have the executable permission to be set. You can do this via the vim function setfperm (:help setfperm()):

:call setfperm('myscript.py', 'rwxr--r--')

After the above steps, you can now run your script anywhere at any time:

:!myscript.py

Enjoy!

Clone this wiki locally