Python can't find my module

Python can't find my module

python-can recv
python-can vector example
python-can tools
python-can listener example
python-can threadsafebus
python-can adapter
python-can slcan
python-can filter

I have a python project (which I run within a virtualenv) and that has the following structure:

Project
├───.git
├───venv
└───src
    ├───__init__.py
    ├───mymodules
    │   ├───__init__.py
    │   ├───module1.py
    │   └───module2.py
    └───scripts
        ├───__init__.py
        └───script.py

script.py

import src.mymodules.module1
...

I run the project with venv activated and from the Project directory using the following command:

(venv)$ python src/scripts/script.py

The script runs but gives out the following error before exiting:

Traceback (most recent call last):
  File "src/scripts/script.py", line 1, in <module>
    import src.mymodules.module1
ImportError: No module named src.mymodules.module1

I have tried running the python shell and trying to import the module from there and it gave no errors. I have _ _init__.py in every directory within src. Is python considering the working directory to be src/scripts? Why is that happening and how can I make src the working directory if that's the case?


Essentially, when you execute script.py directly, it doesn't know that it's part of a submodule of src, nor does it know where a module named src might be. This is the case in either python 2 or 3.

As you know, Python finds modules based on the contents of sys.path. In order to import any module, it must either be located in a directory that's listed in sys.path, or, in the same directory as the script that you're running.

When you say python src/scripts/script.py, sys.path includes the Project/src/scripts/ (because that's where script.py is located), but not Project. Because Project isn't in the path, the modules in that directory (src) aren't able to be imported.

To fix this:

I'm assuming that your script.py is an entry point for your src module (for example, maybe it's the main program). If that's true, then you could fix it by moving script.py up to the same level as src:

Project
├───.git
├───venv
|───script.py       <--- script.py moves up here
└───src
    ├───__init__.py
    └───mymodules
        ├───__init__.py
        ├───module1.py
        └───module2.py

This way, script.py can freely import anything in src, but nothing in src can import script.py.

If that's not the case, and script.py really is a part of src, you can use python's -m argument to execute script.py as part of the src module like so:

$ python -m src.scripts.script

Because you've told python which module you're running (src), it will be in the path. So, script.py will be aware that it's a submodule of src, and then will be able to import from src.

Be careful in this situation though - there's potential to create a circular import if something in src imports src.scripts.script.


As an alternative to both of these approaches, you can modify the sys.path directly in script.py:

import sys
sys.path.insert(0, '/path/to/Project') # location of src 

While this works, it's not usually my preference. It requires script.py to know exactly how your code is laid out, and may cause import confusion if another python program ever tries to import script.py.

python-can · PyPI, The can package provides controller area network support for Python developers​; providing common abstractions to different hardware devices, and a suite of  The can package provides controller area network support for Python developers; providing common abstractions to different hardware devices, and a suite of utilities for sending and receiving messages on a can bus. The library supports Python 2.7, Python 3.5+ as well as PyPy 2 & 3 and runs on Mac, Linux and Windows.


Project
├───.git
├───venv
└───src
    ├───__init__.py
    ├───mymodules
    │   ├───__init__.py
    │   ├───module1.py
    │   └───module2.py
    └───scripts
        ├───__init__.py
        └───script.py

Alternatively you can import like the following in your script.py

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__),'../../'))
import src.mymodules.module1

Now you can run script.py file from any location.

e.g :
python script.py
python /path to folder/script.py

hardbyte/python-can: The can package provides controller , The python-can library provides Controller Area Network support for Python, providing common abstractions to different hardware devices, and a suite of utilities  python-can The C ontroller A rea N etwork is a bus standard designed to allow microcontrollers and devices to communicate with each other. It has priority based bus arbitration and reliable deterministic communication. It is used in cars, trucks, boats, wheelchairs and more.


Another solution is creating file name 'xxx(any name).pth' and write your project dir (mother dir of your src). Put this file in '/virtual_env/lib/pythonXX/site-packages/'. In this way, you don't need to import sys.path in your script.

[PDF] python-can, Message filtering can be set up for each bus. Where the interface supports it, this is carried out in the hardware or kernel layer - not in Python. 3.2  python-can runs any where Python runs; from high powered computers with commercial CAN to usb devices right down to low powered devices running linux such as a BeagleBone or RaspberryPi. More concretely, some example uses of the library: •Passively logging what occurs on a CAN bus.


If you face this problem when dealing with Pytest or coverage. Adding __init__.py file solve most of the cases.

Receive message on CANbus always returns same value with , I have a CAN bus (PCAN) with several inputs. I try to read the inputs in python and print them on the console. The first message I get from the bus  Description. The Controller Area Network is a bus standard designed to allow micro controllers and devices to communicate with each other. It has priority based bus arbitration, reliable deterministic communication.


PiCAN2, Connection are made via DB9 or 3-way screw terminal. There is an easy-to-​install SocketCAN driver, and programming can be accomplished in C or Python. The main objects are the BusABC and the Message . A form of CAN interface is also required. Check the backend specific documentation for any implementation specific details. The Notifier object is used as a message distributor for a bus. Manages the distribution of can.Message instances to listeners. Supports multiple buses and listeners.


Introduction to Python, Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can  Reasonably modern Linux Kernels (2.6.25 or newer) have an implementation of socketcan. This version of python-can will directly use socketcan if called with Python 3.3 or greater, otherwise that interface is used via ctypes.


Python Can :: Anaconda Cloud, The can package provides controller area network support for Python developers​; providing common abstractions to different hardware devices , and a suite of  Parameters: channel – The can interface name.An example would be ‘PCAN_USBBUS1’ Default is ‘PCAN_USBBUS1’ state (can.bus.BusState) – BusState of the channel.. Default is AC