D language import local module - d

I am new to D language. i have a project A. When i executed dub build it has created me libA.so. i am trying to create a different .d file and import this module.
now i wanted to write a D wrapper to consume it.
when i tried
import a
it did not work. i copied the libA.so to the sudo cp libA.so /usr/include/dmd/druntime/import/ but the import did not work.
Can you please help me how can i import this ?

The official documentation for writing and using shared libraries in D on Linux is here:
https://dlang.org/articles/dll-linux.html

You don't import shared libraries in D. You import modules (source files). So the .so file does not belong in the import directory. You need to link to that. import a means the compiler will search the import path for a.d. That's what needs to be in your import directory. If your library has multiple source files, they each need to be on the import path and imported separately.

The question is a bit unclear, but it sounds like you want to create a library via dub, and then use that library in another project. Is that correct?
For example, your project may have a structure like:
myProject
├- commonLib
|
├- program1
| (depends on commonLib)
└- program2
(depends on commonLib)
If this is what you are trying to do, you should consider using Dub sub-packages.
For example, myProject/dub.sdl might look like this:
name "myProject"
targetType "none"
dependency "myProject:common" path="."
dependency "myProject:program1" path="."
dependency "myProject:program2" path="."
subPackage "./common/"
subPackage "./program1/"
subPackage "./program2/"
Your common library at myProject/common/dub.sdl may look like this:
name "common"
targetType "library"
And finally you would depend on it like this in myProject/program1/dub.sdl:
name "program1"
targetType "executable"
dependency "myProject:common" path="../"

Related

How to import test util files inside of your tests on Dart/Flutter

I have some unit tests and some classes that are related only to tests, like factories for my models, some extensions on mockito and others...
The issue is when I try to import those files from my tests I can just import them with relative paths, lets say I have this utilities:
test/src/test_utils/factories.dart
test/src/test_utils/mocks.dart
test/src/test_utils/mockito_extensions.dart
How I can not do it
But When I try to import them from my tests It can not find them with
import 'package:myapp/test_utils/mocks.dart';
or:
import 'package:myapp/src/test_utils/mocks.dart';
The only one that works
The only way to import them is by relative paths like:
import '../../test_utils/mocks.dart';
The question
I would love to understand what is happening here, why my tests cant find the test utils class on the imports and whats the best way to do this.
In the pub package layout the package: URI format is used for files under lib/. These are the "public libraries" that can be used from outside the package, and so they are the ones associated with the package name. Files under test/ are private to the package and don't need to be referenced by package name - they only can be referenced from neighboring files and directories so a relative import is sufficient. The package: URI format is designed primarily because it allows imports to other packages, to which there is no stable relative path.
See https://dart.dev/tools/pub/package-layout#public-libraries
Using relative imports is the recommended approach.

Unable to import sikuli library in RIDE

I have to write automation scripts using python and Robot framework. I have installed python, Robotframework, RIDE, wxpython. I have installed sikuli library but when I import it in my project, library is not imported. I have tried 'Import Library Spec XML'. My question is from where do I import this .xml or how do I create it?
You might need to declare the Pythonpath on RIDE preferences (Tools>Preferences>Importing), you must add there your libraries location, it helped me with suds library.
I have added there:
C:\Python27\lib\site-packages\robot\libraries
(This could be little different for you if you have installed your python at different location)
Edit:
This could be an issue on the way you are trying to import your library, or you need to download a specific sikuli library that work with robot.
Please check then if you are loading your library like this:
*** Settings ***
Library SikuliLibrary
got it from: https://github.com/rainmanwy/robotframework-SikuliLibrary
First check whether Sikuli is installed in python directory's \Lib\site-packages.
Robot test should contain as below:
* Settings *
Documentation Sikuli Library Demo
Library SikuliLibrary mode=NEW
* Test Cases *
Sample_Sikuli_Test
blabh blabh etc

Importing module not in path relative to current file

I'm working on a system where I can't add a new module by adding it's path to sys.path. Instead, I want to place the module in the same folder as the files using it, and then import the module on runtime using imp or importlib (or similar).
I've tried to use both imp and importlib, but can not get it to work. Time will tell if I'm just misinterpreting how and what params to specify when using either of the two libraries.
The folder structure for my project is defined like this:
root-folder-in-sys-path/
- file1.py
- file2.py
- file3.py
- my-module/
--- __init__.py
--- helper1.py
--- helper2.py
As my example indicates, the root folder is part of sys.path. The files (file1.py etc.) is part of the system and is from where I need access to the module. Only files that contains classes of a specific type is added, so it will not be possible just to add the module files in the root to load them, as they will be ignored. Best case would be if helper1.py to helper-n.py is made available - otherwise It is ok if only one is loaded.
Thanks.
I was able to come up with a solution that loads anyone of the helpers. I guess it could easily be made into a package with many more modules this way as well.
To for example access helper1.py in file1.py, this will work:
import os.path, imp
path = os.path.abspath(os.path.join(os.path.dirname(__file__), "my-module/helper1.py"))
im = imp.load_source("helper1", path)
If you find a better solution, then please let me know!

Trouble importing shared object in Python

I am attempting to import a shared object into my python code, like so:
import bz2
to which I get the following error:
ImportError: ./bz2.so: cannot open shared object file: No such file or
directory
Using the imp module, I can verify that Python can actually find it:
>>> import imp
>>> imp.find_module('bz2')
(<open file 'bz2.so', mode 'rb' at 0xb6f085f8>, 'bz2.so', ('.so', 'rb', 3))
The shared object file is in my PYTHONPATH and my LD_LIBRARY_PATH.
Any insights into why I can't import this shared object? Thanks!
bz2.so is the shared object the provides the bzip functionality (which was written in C) for the python modules. You don't import it directly when you do import bz2 , you are actually importing a python module called bz2 which then uses the .so file.
This usually means you haven't got the development version of the bzip library installed or you don't have a c compiler setup for the pip installer to use to build this for you.
You don't say which linux you are using but the general pattern is look in the package manager for bzip2 dev or devel packages and install those.

Where Should Shared Object Files Be Placed?

I am venturing into the land of creating C/C++ bindings for Python using pybindgen. I've followed the steps outlined under "Building it ( GCC instructions )" to create bindings for the sample files:
http://packages.python.org/PyBindGen/tutorial.html#a-simple-example
Running make produces a .so file. If I understand how .so files work, I should be able to import the classes in the shared object into Python. However, I'm not sure where to place the file and how to let Python know where it is. Additionally, do the original c/c++ source files need to accompany the .so file?
So far I've tried placing the file in /usr/local/lib and adding that path to DYLD_LIBRARY_PATH to the .bash_profile. When I try to import the module from within the Python interpeter an error is thrown stating that the module can not be found.
So, my question is: What needs to be done with the generated .so file in order for it to be used by a Python program?
Python looks for .so modules in the same directories where it searches python ones. So you have to install it as you would normal python module either somewhere that is on python's sys.path by default (/usr/share/python/site-lib or something like that—it'd distribution-dependent) or add the directory to PYTHONPATH environment variable.
It's python that's loading the module using dlopen, not the dynamic linker, so LD_LIBRARY_PATH (note, there is no DY) won't help you.
Same as all other Python modules. It must be within one of the locations given in sys.path.