0

I wanted to write a test program and for this I need to include my DPS310.py file. But somehow it doesn't work

I tried:

• from myPackage import DPS310
• import ..DPS310
• import myPackage.DPS310

My structure:

Projekt
├── myPackage
│   ├── __init__.py
│   └── DPS310.py
├── tests
│   ├── __init__.py
│   └── test_module1.py
├── README.md
├── LICENSE
└── setup.py

test_module1.py File

import myPackage.DPS310


msg = "Hello World"
print(msg)

dps = DPS310()
#y = DPS310.getTemperature

print(dps.getTemperature())

DPS310.py File (Extraction. Just to show that the getTemperature method is in here)

...
class DPS310():
    def __init__(self, *args):
        self.i2c = smbus2.SMBus(1)
        if len(args) > 0:
            self.addr = args[0]
            print("Man addr set")
        else:
            # default sensor's address
            self.addr = 0x77

    def getTemperature(self):
        r = self.i2c.read_i2c_block_data(self.addr, DPS310_TMP_B2, 3)
        # reads out the temperature that is stored in the register
        #       r[0]=TMP0-7    r[1]=TMP8-15  r[2]=TMP16-23
        temp = (r[2] << 16) | (r[1] << 8) | (r[0])  # r[0] << 0
        # deploys this function to 24 bits
        return self.twos_comp(temp, 24)
...

If I run the test_module1.py file:

Exception has occurred: ModuleNotFoundError
  No module named 'myPackage'
    File "C:\Julian\Projects\PhytonLib_Raspberry\DPS310_Library_RPi\tests\test_module1.py", line 1, in <module>
      import myPackage.DPS310
martineau
  • 119,623
  • 25
  • 170
  • 301
JulianS
  • 41
  • 3

1 Answers1

0
No module named 'myPackage' File

Most likely Python doesn't know where to find myPackage, so you could try setting it as a source root (you could look up the relevant info for your IDE). Also, check out this StackOverFlow post.

MorganStark47
  • 154
  • 2
  • 15
  • I also use Pycharm. okey i have marked **myPackage** as my Source folder. Its blue now. But the **"import DPS310"** still doesn't work – JulianS Sep 24 '19 at 07:53
  • Yes. **import myPackage.DPS310** >File "C:/Users/Schmeizl/PycharmProjects/DPS310-Library-RPi/tests/test_module1.py", line 1, in import myPackage.DPS310 I tryed this then: dps = myPackage.DPS310() and y = dps.getTemperature() – JulianS Sep 24 '19 at 08:14