colcon-core
colcon-core is the foundational Python package for 'colcon', a command-line tool designed to streamline the workflow of building, testing, and using multiple software packages, especially within robotics frameworks like ROS. It automates package discovery, dependency resolution, and environment setup. The library is currently at version 0.20.1 and follows a continuous release model, with new versions typically released as needed to support updates in related projects like ROS.
Common errors
-
ERROR:colcon.colcon_core* is declared multiple times
cause This error typically occurs when `colcon-core` and an extension package (like `colcon-override-check`, implicitly installed by `colcon-common-extensions`) both attempt to provide the same functionality or argument, often due to version mismatch or the functionality being refactored into a separate package.fixEnsure all `colcon` related packages (e.g., `colcon-core`, `colcon-common-extensions`, `colcon-override-check`) are up-to-date. If installed via `apt`, run `sudo apt update && sudo apt upgrade`. If via `pip`, try `pip install --upgrade --upgrade-strategy eager colcon-common-extensions`. -
ImportError: cannot import name 'OVERRIDE_OPT' from 'em'
cause `colcon-core` depends on `empy` for templating, but `empy 4.0` introduced breaking changes, removing `OVERRIDE_OPT`.fixInstall a compatible version of `empy`. The fix is usually `pip install 'empy<4'` or specifically `pip install empy==3.3.4`. -
E: Unable to correct problems, you have held broken packages. The following packages have unmet dependencies: python3-colcon-core : Depends: python3 (>= 3.8) but 3.6.7-1~18.04 is to be installed or python3-importlib-metadata but it is not installable
cause `colcon-core` version 0.13.1 and newer requires Python 3.8+ or the `importlib-metadata` backport, which is not available via `apt` on older Ubuntu distributions (e.g., 18.04 with Python 3.6).fixInstall `colcon-core` and its extensions via `pip` (e.g., `pip install colcon-common-extensions`) instead of `apt` on affected systems, as `pip` will automatically install the `importlib-metadata` backport. Alternatively, upgrade your operating system and Python version to 3.8 or newer.
Warnings
- breaking Incompatibility with `empy` version 4.0 and higher due to breaking API changes in `empy`. This can lead to `ImportError: cannot import name 'OVERRIDE_OPT' from 'em'` or similar failures.
- breaking `colcon-core` versions from `0.13.1` introduced a dependency on Python 3.8's `importlib.metadata` or the `importlib-metadata` backport. This broke installations via `apt` on older Ubuntu versions (e.g., 18.04 with Python 3.6) which lacked these packages, resulting in 'unmet dependencies' errors.
- gotcha When using `--symlink-install`, `colcon-core` may be incompatible with `setuptools >= 80.0`, leading to build failures or incorrect installations.
- gotcha `colcon build` might use an unexpected Python interpreter if not explicitly invoked from within an activated virtual environment, particularly on Windows, causing `ModuleNotFoundError` for packages expected to be in the virtual environment.
Install
-
pip install colcon-core -
pip install colcon-common-extensions
Imports
- PackageIdentificationExtensionPoint
from colcon_core.package_identification import PackageIdentificationExtensionPoint
- EnvironmentExtensionPoint
from colcon_core.environment import EnvironmentExtensionPoint
- satisfies_version
from colcon_core.plugin_system import satisfies_version
Quickstart
import os
# Create a dummy workspace and a simple Python package
os.makedirs('my_workspace/src/my_python_package', exist_ok=True)
with open('my_workspace/src/my_python_package/setup.py', 'w') as f:
f.write("""
from setuptools import setup
package_name = 'my_python_package'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages', ['resource/' + package_name]),
('share/' + package_name, ['package.xml'])
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='Your Name',
maintainer_email='you@example.com',
description='A minimal Python package for colcon',
license='Apache-2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'my_node = my_python_package.main:main'
],
},
)
""")
with open('my_workspace/src/my_python_package/package.xml', 'w') as f:
f.write("""
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>my_python_package</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="you@example.com">Your Name</maintainer>
<license>Apache-2.0</license>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
""")
with open('my_workspace/src/my_python_package/my_python_package/main.py', 'w') as f:
f.write("""
def main():
print('Hello from my_python_package!')
if __name__ == '__main__':
main()
""")
os.makedirs('my_workspace/src/my_python_package/resource', exist_ok=True)
with open('my_workspace/src/my_python_package/resource/my_python_package', 'w') as f:
f.write('')
# Now, build the workspace using colcon
print('\n--- Running colcon build ---')
os.system('cd my_workspace && colcon build --packages-select my_python_package')
# Source the setup file and run the node
print('\n--- Sourcing setup and running node ---')
# For simplicity, we'll try to run directly if possible, or instruct sourcing.
# In a real shell, you would run: . install/setup.bash (or .ps1, .bat)
# For a self-contained python script, it's complex to source a shell file.
# Instead, we will try to invoke the installed script directly if available
node_path = 'my_workspace/install/my_python_package/lib/my_python_package/my_node'
if os.path.exists(node_path):
os.system(node_path)
else:
print("To run the node: cd my_workspace && . install/setup.bash && my_node")