Installing P4 API for Python

There are several ways to install P4 API for Python:

In each case, ensure you have met all the criteria of the prerequisites

Prerequisites

  • Ensure your machine meets the system requirements. For information, see System Requirements and Release Notes.

  • The latest version of P4 API for Python requires Python 3.13 and above.
  • Ensure old versions of P4 API for Python are uninstalled before installing the latest version. To uninstall P4 API for Python, ensure the following files are deleted:
    • p4.py

    • p4.pyc

    • p4.pyo

    • P4Client.pyd

Install P4 API for Python using pip

The easiest way to install P4 API for Python is to install the package using pip.

  1. Open a terminal or command prompt.

  2. (Optional) Upgrade pip using the following command:

    python -m pip install --upgrade pip

  3. Install P4 API for Python using the following command:

    pip install p4python

  4. If P4 API for Python installed correctly, you will see the following output in the terminal or command prompt:

    Successfully installed P4Python-<VERSION>

    Where <VERSION> is the installed version of P4 API for Python.

  5. (Optional) To test that P4 API for Python has been installed correct, run the following command:

    python -c "import P4; print('P4Python installed!')"

    If P4 API for Python has been installed correctly, you will see the following output:

    P4Python installed!

The pip installer installs binary versions of P4 API for Python where possible, otherwise it attempts to automatically build P4 API for Python from source. For instructions on building P4 API for Python from source, see Install P4 API for Python from source

Additional installation options

When installing P4 API for Python, the setup process may need to download extra files to complete the build.

P4 API for C/C++ Download

When P4 API for Python is built without the --apidir option, the installer will try to download the correct version of the P4 API for C/C++ from the Perforce website. These files are unpacked into a temporary folder during setup.

OpenSSL on Linux

If you installed P4 API for Python on Linux and don’t use the --ssl option, the installer will check your system for a compatible version of OpenSSL.

  • For P4 API for Python 2019.1 and later, OpenSSL is required.

  • The supported versions of OpenSSL are 1.0.2+ or 1.1.1+

  • If no supported version is found, the installer will automatically download one from ftp.openssl.org.

Install P4 API for Python on Linux using system packages

Depending on the version of Linux you have determines how to install P4 API for Python

Prerequisite: Set Up the Perforce Package Repository

Before installing P4 API for Python, you must configure the Perforce package repository for your Linux distribution.

Install with Ubuntu

Create the file /etc/apt/sources.list.d/perforce.list with the appropriate content:

Ubuntu 20.04:

deb http://package.perforce.com/apt/ubuntu/ focal release

Ubuntu 22.04:

deb http://package.perforce.com/apt/ubuntu/ jammy release

Ubuntu 24.04:

deb http://package.perforce.com/apt/ubuntu/ noble release

Then import the signing key and update:

wget -qO - https://package.perforce.com/perforce.pubkey | sudo apt-key add -
sudo apt-get update

Install with RHEL / Rocky Linux

Create the file /etc/yum.repos.d/perforce.repo with the following content:

RHEL 8/Rocky Linux 8:

[Perforce]
name=Perforce
baseurl=http://package.perforce.com/yum/rhel/8/x86_64/
enabled=1
gpgcheck=1

RHEL 9/Rocky Linux 9:

[Perforce]
name=Perforce
baseurl=http://package.perforce.com/yum/rhel/9/x86_64/
enabled=1
gpgcheck=1

RHEL 10

[Perforce]
name=Perforce
baseurl=http://package.perforce.com/yum/rhel/10/x86_64/
enabled=1
gpgcheck=1

Import the Perforce package signing key:

sudo rpm --import https://package.perforce.com/perforce.pubkey

For information about how to verify the authenticity of the signing key, see Perforce packages.

Install P4 API for Python

To install P4 API for Python quickly on any supported Linux distribution, use the meta-package:

sudo apt install perforce-p4python3      # Ubuntu/Debian
sudo yum install perforce-p4python3      # RHEL/Rocky Linux

This package automatically installs the correct version of P4 API for Python for your system's default Python 3 interpreter.

When passing arguments, make sure to omit the space between the argument and its value, such as in the value pair -u and username in the following example:

change = p4.run_changes("-uusername", "-m1")[0]

If you include a space ("-u username"), the command fails.

Optional: Install Specific Python Versions

If you need to install P4 API for Python for a specific Python version, use one of the following packages:

Ubuntu

  • perforce-p4python3-python3.8

  • perforce-p4python3-python3.9

  • perforce-p4python3-python3.10

  • perforce-p4python3-python3.12

RHEL / Rocky Linux

  • perforce-p4python3-python3.8

  • perforce-p4python3-python3.9

P4 API for Python 2025.2 is the last release to support and test against Python 3.9, which is in EOL status.

Install P4 API for Python from source

The official version of P4 API for Python exists as source on GitHub.

Instructions for building are in the BUILD.md file, also hosted on GitHub.

Sample script to test P4 API for Python

To further check if P4 API for Python has been installed correctly, create a simple script that connects to your server.

Create test_p4.py in a folder (for example, C:\p4python) with the following code:

Copy
from P4 import P4, P4Exception

# Configure Perforce
p4 = P4()
p4.port = "perforce:1666"
p4.user = "user.name" # update with your username
p4.client = "workspace" # update with your workspace

try:
    p4.connect()
    print("Connected to Perforce server:", p4.port)
    print("Current client workspace:", p4.client)
except P4Exception as e:
    print("Could not connect. Error:", e)
finally:
    p4.disconnect()

To run the script:

  1. Open Command Prompt and navigate to your script folder:

     cd c:\p4python
  2. Run the following:

     python test_p4.py
  3. If P4 API for Python successfully connected you will see the following output:

    Connected to Perforce server: perforce:1666
    Current client workspace: workspace

    If you see Could not Connect. Error, check that you are on the VPN and your P4PORT is correct.