如何解决Python3.6与3.7下CANard和can-isotp的兼容问题?
Alright, let's tackle this Python version compatibility head-on—you're stuck between needing Python 3.6 for CANard and 3.7+ for can-isotp, and you need both to handle different CAN message types. Here are four solid solutions to get you up and running:
1. Use Virtual Environments to Isolate Python Versions
This is the simplest, cleanest approach to keep both environments separate without conflicting with each other:
- First, confirm both Python 3.6 and 3.7 are installed on your system. If you're missing Python 3.7, install it via your system's package manager or the official Python installer.
- Create a virtual environment for Python 3.6 (for CANard):
python3.6 -m venv canard-env - Activate the environment and install CANard:
# Linux/macOS source canard-env/bin/activate # Windows canard-env\Scripts\activate pip install CANard - Create a separate virtual environment for Python 3.7 (for can-isotp):
python3.7 -m venv isotp-env - Activate it and install can-isotp:
source isotp-env/bin/activate # or Windows equivalent pip install can-isotp - Write two dedicated scripts: one for 8-byte standard CAN messages (run with
canard-env/bin/python script-canard.py) and one for 128-byte extended messages (run withisotp-env/bin/python script-isotp.py). If you need a single entry point, add a wrapper script that calls both using their respective virtual environment interpreters.
2. Check for Backported or Compatible Library Versions
Sometimes you don't need separate environments—you just need to find library versions that work across your required Python versions:
- For can-isotp: Check if older versions support Python 3.6. Try installing specific versions in your Python 3.6 environment:
You can look up the package's release notes to identify compatible versions (many maintainers drop older Python support in newer releases, so an older tag might work).pip install can-isotp==1.0.0 # Replace with a version known to support 3.6 - For CANard: The
pip3.7: command not founderror might mean your Python 3.7 is installed aspython3instead ofpython3.7. Try installing CANard with:
If that fails, try installing from source—sometimes source installs work even if the PyPI package isn't updated for newer Python versions:python3 -m pip install CANardgit clone https://github.com/ericevenchick/CANard.git cd CANard python3.7 setup.py install
3. Split Functionality into Separate Scripts with Inter-process Communication (IPC)
If you need both features to work in a single workflow, split them into standalone scripts and use IPC to connect them:
- Create two scripts:
canard_sender.py: Handles 8-byte standard CAN messages (runs with Python 3.6)isotp_sender.py: Handles 128-byte extended CAN messages (runs with Python 3.7)
- Use simple IPC methods to coordinate them:
- Subprocess calls: Write a main script that triggers the other scripts using the correct interpreter. Example:
# main.py import subprocess # Send 8-byte message via CANard subprocess.run(["/path/to/canard-env/bin/python", "canard_sender.py", "--message", "your_8byte_data"]) # Send 128-byte message via can-isotp subprocess.run(["/path/to/isotp-env/bin/python", "isotp_sender.py", "--message", "your_128byte_data"]) - Named pipes (FIFOs) or TCP sockets: For real-time communication, set up a socket server in one script and a client in the other to pass data back and forth seamlessly.
- Subprocess calls: Write a main script that triggers the other scripts using the correct interpreter. Example:
4. Use Docker Containers for OS-Level Isolation
If you need consistent, shareable environments (especially for team work), Docker containers are a great option:
- Create two Dockerfiles:
- For CANard (Python 3.6):
FROM python:3.6-slim WORKDIR /app COPY requirements-canard.txt . RUN pip install --no-cache-dir -r requirements-canard.txt COPY canard_sender.py . CMD ["python", "canard_sender.py"] - For can-isotp (Python 3.7):
FROM python:3.7-slim WORKDIR /app COPY requirements-isotp.txt . RUN pip install --no-cache-dir -r requirements-isotp.txt COPY isotp_sender.py . CMD ["python", "isotp_sender.py"]
- For CANard (Python 3.6):
- Build and run each container as needed. To access the CAN bus from inside the container, pass the CAN interface device (e.g.,
--device=/dev/can0on Linux) when starting the container.
My top recommendation is virtual environments (solution 1) because it's lightweight, easy to set up, and keeps your system Python installation clean. If you need tighter integration between the two functionalities, go with the IPC approach (solution 3).
内容的提问来源于stack exchange,提问作者annonymous

