Creating a Perforce ClientSpec with P4Python

We’re beginning our migration to Perforce at work and I’ve been doing most of the scripting work since. . .well, since I’m apparently the expert. I’m using Python and the P4Python API to Perforce and I ran into a little issue that isn’t documented in the P4Python documentation. It makes sense after the fact but it took me a while to figure it out. I thought it might be useful to someone if I wrote it up here.

When you’re dealing with the client specification’s details in P4Python, you reference them as a dictionary. Pretty much all the values in the dictionary are strings (Root, User, etc) and I kept trying to make the View a string that I built up from scratch. I just assumed that the multiple lines in the view should be manually created using a new line character. However, as in everything else Python, it’s easier than that. The View should be a list of the mappings you want to include in the client spec. The example below shows how to use P4Python to create a new workspace in Perforce.


import p4

p4c = p4.P4()
p4c.parse_forms()

try:
  p4c.connect()
  spec = p4c.fetch_client("bbim-MySampleClient")
  spec["Root"] = "C:/Workspace/MySample"
  view = []
  view.append("//main/Workspace/BrettBim/MySample/...  //bbim-MySampleClient/...")
  view.append("//main/NUnit/nunit.framework.dll  //bbim-MySampleClient/tools/nunit/nunit.framework.dll")
  spec["View"] = view
  p4c.save_client(spec)
  p4c.run_sync()

except p4.P4Error:
  # If any errors occur, we'll jump in here. Just log them
  # and raise the exception up to the higher level
  for e in p4.errors:
    print e


It makes sense that the view is a list since you can have multiple lines to map several regions of the depot to your workspace but it would be a nice addition to the documentation to detail that.

0 comments on “Creating a Perforce ClientSpec with P4Python

  1. Hello,

    When i ran your script, i faced this error. Could you help me to check this problem:

    File “createws.py”, line 7, in
    p4.parse_forms()
    File “/usr/local/lib/python2.7/dist-packages/P4.py”, line 402, in
    return lambda *args, **kargs: self.parse_spec(cmd, *args, **kargs)
    TypeError: function takes exactly 2 arguments (1 given)

    Thanks in advance,

  2. Hello,

    When i ran your script, i faced this error. Could you help me to check this problem:

    File “createws.py”, line 7, in
    p4.parse_forms()
    File “/usr/local/lib/python2.7/dist-packages/P4.py”, line 402, in
    return lambda *args, **kargs: self.parse_spec(cmd, *args, **kargs)
    TypeError: function takes exactly 2 arguments (1 given)

    Thanks in advance,

  3. Hello,
    When i ran your script, i faced this error. Could you help me to check this problem:
    File “createws.py”, line 7, in
    p4.parse_forms()
    File “/usr/local/lib/python2.7/dist-packages/P4.py”, line 402, in
    return lambda *args, **kargs: self.parse_spec(cmd, *args, **kargs)
    TypeError: function takes exactly 2 arguments (1 given)
    Thanks in advance,

  4. Hello,
    When i ran your script, i faced this error. Could you help me to check this problem:
    File “createws.py”, line 7, in
    p4.parse_forms()
    File “/usr/local/lib/python2.7/dist-packages/P4.py”, line 402, in
    return lambda *args, **kargs: self.parse_spec(cmd, *args, **kargs)
    TypeError: function takes exactly 2 arguments (1 given)
    Thanks in advance,

Leave a Reply

Your email address will not be published. Required fields are marked *