Running the PythonBridge in another directory

Normally the runs in a directory next to the image called PythonBridgeRuntime.

PBPlatform current workingDirectory
  

You have to change this setting after creating a new PBApplication LanguageLinkApplication subclass: #PBApplication instanceVariableNames: '' classVariableNames: 'DebugMode' package: 'PythonBridge-Global' instance but before actually starting it.

This is not possible with the LanguageLinkApplication>>#uniqueInstance uniqueInstance ^ uniqueInstance so we have to take a little detour.

We'll be using the following directory. This does not have to exist.

alternativeWorkingDirectory := FileLocator temp / 'PythonBridgeRuntine'
  

First create a new instance.

pbApplication := PBApplication new
  

Now tell this instance to use our new directory.

pbApplication settings workingDirectory: alternativeWorkingDirectory
  

When we start our new instance, a new installtion will happen in that directory.

pbApplication start
  

We can test our application using the API.

pbApplication newCommandFactory
	<< #sys import;
	<< (#sys asP3GIdentifier => #path);
	sendAndWait
  

In order for Python snippets to work, you need to adopt our instance as the default global unique instance.

PBApplication uniqueInstance: pbApplication
  

Now, Python snippets should work as they normally do, but now using the instance that we configured to run in our alternative directory.

import sys
sys.path
  

The indicator top right above did not pickup the fact that the bridge is actually running, you can fix this by restarting the instance programmatically once it is the global default.

PBApplication uniqueInstance restart
  

We can write a simple Python source code file and try to use it to verify that we are really running from the new directory. You can of course also inspect the internal details of the bridge instance.

alternativeWorkingDirectory / 'foo.py' writeStreamDo: [ :out |
	out 
		<< 'def Foo():'; lf;
		tab; << 'return 42'; lf ]
  
import foo
foo.Foo()
  

Beware that stopping the global application using the class side LanguageLinkApplication>>#stop stop uniqueInstance ifNotNil: [ uniqueInstance stop. ]. uniqueInstance := nil resets everything.