I am looking for a way to import custom python package which has a hyphen in its name. I thought hyphen is not an recommended approach but since its a shared project I do not have control over it.
Think here is, I wanted to add a bash script which checks after installing all packages that the installation is successful.
I am able to verify it using:
<PYTHON_VERSION> -c "import <PACKAGE_NAME>" &> /dev/null
if [[ $? -ne 0 ]]; then
echo "Failed"
fi
I see that I am able to install this custom package successfully, since I checked in the virtual environment using:
>>> import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
And also did a pip freeze
to see installed packages.
However while importing I see error such as:
>>> import build-config
File "<stdin>", line 1
import build-config
^
SyntaxError: invalid syntax
>>> import build_config
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'build_config'
Any pointers please?
You can use the importlib
module.
Import using the importlib.import_module(name, package=None)
function like this
import importlib
module_name = importlib.import_module('module-name')
Then use module_name
to access it.