External libraries and dependencies: Difference between revisions
m (Afischer moved page Device Class Dependencies to External libraries and dependencies: more clear) |
No edit summary |
||
Line 6: | Line 6: | ||
For that reason, we recommnend to use our Library Builder which adds all missing packages to a folder which you can copy into your Device Class folder. | For that reason, we recommnend to use our Library Builder which adds all missing packages to a folder which you can copy into your Device Class folder. | ||
The Library Builder is available through your sweep-me.net user account. Please contact us to get one (contact@sweep-me.net). | The Library Builder is available through your sweep-me.net user account. Please contact us to get one (contact@sweep-me.net). | ||
== Library Builder == | |||
=== Requirements === | === Requirements === | ||
* Python 3.6 | * Python 3.6 (same version as used by SweepMe!) | ||
* All packages installed you need for your Device Class | * All packages installed you need for your Device Class | ||
* pyinstaller 3.3.1 [https://pypi.org/project/PyInstaller/3.3.1/] (newer versions will not work) | * pyinstaller 3.3.1 [https://pypi.org/project/PyInstaller/3.3.1/] (newer versions will not work) | ||
Line 15: | Line 18: | ||
=== Extracting the missing packages === | === Extracting the missing packages === | ||
# Goto LibraryBuilder folder | # Goto LibraryBuilder folder | ||
# Open build_library.py and change '''sweepme_path''' to your installation path of SweepMe! | # Open build_library.py and change '''sweepme_path''' to your installation path of SweepMe! | ||
# Open required_modules.py and import all packages which are missing in SweepMe! | # Open required_modules.py and import all packages which are missing in SweepMe! | ||
Line 21: | Line 24: | ||
# Copy the folder libs_required_modules to your DC and rename as desired | # Copy the folder libs_required_modules to your DC and rename as desired | ||
# Do not forget to add licence files for each packages you added yourself | # Do not forget to add licence files for each packages you added yourself | ||
== Environment variables PATH and PYTHONPATH == | == Environment variables PATH and PYTHONPATH == |
Revision as of 19:35, 2 June 2019
Python modules
Not all available packages for Python can be part of SweepMe!. Still, you can add these packages to your Device Class and load them from a subfolder. In that case, it might be that sub-dependencies will be shipped twice, once with SweepMe! and once with your Device Class.
For that reason, we recommnend to use our Library Builder which adds all missing packages to a folder which you can copy into your Device Class folder. The Library Builder is available through your sweep-me.net user account. Please contact us to get one (contact@sweep-me.net).
Library Builder
Requirements
- Python 3.6 (same version as used by SweepMe!)
- All packages installed you need for your Device Class
- pyinstaller 3.3.1 [1] (newer versions will not work)
Extracting the missing packages
- Goto LibraryBuilder folder
- Open build_library.py and change sweepme_path to your installation path of SweepMe!
- Open required_modules.py and import all packages which are missing in SweepMe!
- Run build_library.py
- Copy the folder libs_required_modules to your DC and rename as desired
- Do not forget to add licence files for each packages you added yourself
Environment variables PATH and PYTHONPATH
Often, you have to add the folder and subfolders of a DeviceClass to the environment variable PATH or to PYTHONPATH to allow for importing libraries and dlls (dynamic link libraries). We created a convenience function that adds all folders and subfolders of your DeviceClass script to both environment variables, so that all further imports should work immediately.
Just add the following two lines of code to your DeviceClass before you import external libraries that do not included to SweepMe!
from FolderManager import addFolderToPATH
addFolderToPATH()
Adding a folder to PYTHONPATH
Of course, you can add folders yourself, e.g. to load a python package that comes with your DeviceClass being in the subfolder "libs", you can use
# Add the path of your libs within your Device Class to the variable sys.path:
import os
import sys
libpath = os.path.dirname(__file__) + os.sep + "libs"
if not libpath in sys.path:
sys.path.append(libpath)
# Now import your libs as usual
import *name of the lib*
Adding a folder to PATH
In case you like to add a folder to environment variable PATH, use the following lines of code
# Add the path of your libs within your Device Class to the variable sys.path:
import os
import sys
libpath = os.path.dirname(__file__) + os.sep + "libs"
if not libpath in os.environ["PATH"].split(os.pathsep):
os.environ["PATH"] = libpath + os.pathsep + os.environ["PATH"]
DLL
.Net DLL
version: 1.5.3
Loading
Before you can load a .NET dll, one has to make sure that SweepMe! can find it. Either put the dll into the folder 'ExternalLibraries' of the public SweepMe! folder or put it into the folder of your Device Class. In the latter case, the path of the folder needs to be added to the PATH variable, which can automatically be done with the following two lines.
import FolderManager
FolderManager.addFolderToPATH()
If you add the dll file to the folder of your Device Class and you like to share it, please make sure that you have the right to distribute the dll file.
A .NET assembly can be loaded using the pythonnet/clr package that already comes with SweepMe! with
import clr
at the top of the Device Class file.
With the following line, you can add the dll file to the namespace of python:
clr.AddReference('RgbDriverKit')
# use 'RgbDriverKit' if the file has the name 'RgbDriverKit.dll'
If the PATH was not updated correctly in the first step, you get an error message like 'Unable to find assembly'.
Now, you can use the name of the added reference like a python package:
from RgbDriverKit import SimulatedSpectrometer, RgbSpectrometer, SpectrometerStatus
I some cases, the name of the dll and the name of the package that is provided by 'clr.AddReference' are not identical. Check the namespace of clr by
print(clr.__dict__)
in order to see the correct key/name that has to be used during import.
For example, a dll provided by Newport called 'Cornerstone.dll' to control monochromators can be used with the following two lines
clr.AddReference('Cornerstone')
import CornerstoneDll as cs
In this case, the name of the imported dll ('CornerstoneDll') is different from the name of the .NET assembly ('Cornerstone').
In order to see the available function of the .NET dll, you can use the tool 'ildasm.exe' that e.g. comes with Microsoft Visual Studio. Please find further information here: https://docs.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler
Example
The following Device Classes are based on .NET dll and might help you to get startet with your Device class
- Spectrometer-RGBphotonics_Qwave
- Spectrometer-Labsphere_CDS6x0
- Camera-Basler_ace