Thrillofit86
Thrillofit86

Reputation: 619

Import error - Cannot import name x from y

Im having trouble importing a class on a python module.

Here are my directory structure:

TestMap
  +lib
    +vendors
      +testing
       - _init.py
       - products.py
    - _init_py.
    - notifications.py
    - scraper.py
    - utils.py
  -main.py

And I'm starting on scraper.py and trying to get functions on products.py which is vendors -> testing -> products.py

from .vendors.testing.products import TestProducts

and what I am trying to do is:

ImportError: cannot import name 'TestProducts' from 'lib.vendors.testing.products' (C:\Users\Annoynmous\Desktop\TestMap\lib\vendors\testing\products.py)

and inside the products.py the class name is:

class TestProducts():

and I cant get a grip what I am actually doing wrong?

Upvotes: 0

Views: 927

Answers (2)

Ratnesh
Ratnesh

Reputation: 1700

Try this in scraper.py

from vendors.testing.products import TestProducts

Or

import vendors.testing.products as product 
class scraper:
    def __init__(self):
        self.product = product.TestProducts()

Use self.product to access any function of TestProducts inside the class scraper.

Upvotes: 1

Alex
Alex

Reputation: 1375

Use pythonpath to set the source directory in your project:

export PATH=$PATH:/home/user/somepath/TestMap 

and import the modules from that source path

from lib.vendors.testing.products import TestProducts

Or use __init__.py file to define your internal module directory and import the module with direct module name.

Upvotes: 1

Related Questions