0

I'm working with Perforce and have a script to check out various files depending on my current task. The issue is that I'd like to store environment variables for the port, user and client so that I don't have to change my script each time it is run from a different computer.

For accessing Perforce, I've set up as the example posted below and tried to change the client using the formula "p4 set P4CLIENT=value" in CMD where "value" is what "p4.client" would be assigned to in the Python script. I'm unsure if this is how to go about it and would appreciate any help.

--Example--

from P4 import P4,P4Exception    # Import the module
p4 = P4()                        # Create the P4 instance
p4.port = "1666"
p4.user = "fred"
p4.client = "fred-ws"            # Set some environment variables
  • I wouldn't recommend environment variables, for this situation... How about using command line arguments instead? Using argparse library – Jordan Simba Dec 21 '19 at 22:32
  • Is this then something the user would have to type in manually? I was hoping to not have to do that. – sonorityscape Dec 21 '19 at 23:17
  • The use the OS module. It has method ```os.environ['ENV_VAR']``` which allows you to set and unset environment variables.... just make sure they're available to your python process – Jordan Simba Dec 22 '19 at 09:09
  • I can't figure out what you're asking. Do you want the script to use the local environment settings? If so, you don't have to do anything; it'll inherit them automatically. Do you want it to use hardcoded settings? If so, your code sample is exactly the right way to do it (don't set them in the user's environment, it'll mess up their other tools). – Samwise Dec 22 '19 at 22:13
  • 1
    Ohh, thank you Sam! I didn't realize you could omit those values from the script. Yes, I have a workspace at work and home and was under the assumption that those had to dynamically change to access different workspaces. – sonorityscape Dec 22 '19 at 23:24
  • As long as you have them set in your environment normally (i.e. using `p4 set`), every Perforce tool should just automatically pick those settings up, including scripts written with the Perforce client API. Most of the time you shouldn't need to modify the connection settings within the script itself (the exception would be if the script needs to manage its own workspace that's separate from yours, or something like that). – Samwise Dec 23 '19 at 02:34

0 Answers0