There's a large number of examples of using your user/pass as credentials to connect to the p4 server using p4python, but very little describing how to use a p4ticket. It's unclear how to use a p4tickets file with p4python.
3 Answers
It turns out the perforce documentation is really, really precise. The documentation basically says that if the user isn't logged in, then you have to provide a password to login. The flip side of that coin is that if the user is logged in then no password is needed. So, for instance, assume the user is logged in:
>>> p4 -u 'username' login
Then, in your p4python script, the following will connect:
p4con = P4.P4()
p4con.user = 'username'
p4.con.connect()
The p4python connection will naturally use the ~/.p4tickets file.

- 367
- 2
- 13
-
2If you want to find out if you're logged in or not, you can run `p4 login -s` – Bryan Pendleton Jan 11 '18 at 19:03
To use a ticket in a P4 Python script (or the P4 command line) you essentially just put the ticket anywhere that asks for a password. With that, there's an issue of precedence of where P4 Python takes the ticket from that is not consistent with how the P4 command line works.
In P4 Python:
- If the environment variable P4PASSWD is set it is used.
- If there is an entry for the user in whatever file p4.ticket_file looks at (from P4TICKETS, the default of ~/.p4tickets, or a value that was placed there by the script) that will be used if P4PASSWD is not set
- If the p4.password field is set it will be used if neither of the above conditions have been met.
Running P4 from the command line:
- If run with -P the ticket from the command line is used
- If P4PASSWD is set that is used if the above condition has not been met
- If there is an entry for the user in whatever file p4.ticket_file looks at (from P4TICKETS, the default of ~/.p4tickets, or a value that was placed there by the script) that will be used if neither of the above conditions have been met.
Example 1 - This will override the value set in p4.password on line 4 with the value for the user set on line 3 if there is an entry in the ticket file. If there's no entry in the ticket file it will use the supplied credentials AND update the ticket file so that the second time the script is run it will use that and not the supplied credentials.
from P4 import P4
p4 = P4()
p4.user = user
p4.password = ticket
Example 2 - This will always use the supplied credentials because we're not actually using a real file.
from P4 import P4
p4 = P4()
p4.user = user
p4.password = ticket
p4.ticket_file = '/dev/null'
tl;dr - Use the ticket anywhere Perforce asks for a password. If you want to use a ticket in your script, set ticket_file='/dev/null'
first to ensure the expected behaviour.
run_login(): Runs p4 login using a password or ticket set by the user.
from P4 import P4
p4 = P4()
p4.port = "xxx.xxx.xxx.xxx:1666"
p4.user = "xxxxx"
p4.connect()
p4.run_login("-s", p4.user)