user2650277
user2650277

Reputation: 6739

Importing class in same directory

I have a project named AsyncDownloaderTest with main.py and AsyncDownloader.py in same directory.I have just started learning python but it seems issue is with the import.

main.py

from .AsyncDownloader import AsyncDownloader

ad = AsyncDownloader()
ad.setSourceCSV("https://people.sc.fsu.edu/~jburkardt/data/csv/grades.csv","First name")
print(ad.printURLs)

AsyncDownloader.py

import pandas as pd

class AsyncDownloader:

    """Download files asynchronously"""

    __urls = None



    def setSourceCSV(self, source_path, column_name):
        self.source_path = source_path
        self.column_name = column_name
        # TODO Check if path is a valid csv
        # TODO Store the urls found in column in a list
        my_csv = pd.read_csv(source_path, usecols=[column_name], chunksize=10)
        for chunk in my_csv:
            AsyncDownloader.urls += chunk.column_name

    def printURLs(self):
        print(AsyncDownloader.urls)

I am getting the following error

ModuleNotFoundError: No module named '__main__.AsyncDownloader'; '__main__' is not a package

Upvotes: 1

Views: 1818

Answers (1)

Matt Morgan
Matt Morgan

Reputation: 5303

Do you have __init__.py in the same directory as AsyncDownloader.py? That should do it.

__init__.py is an empty file that signals that the directory contains packages and makes functions and classes importable from .py files in that directory.

You can probably lose the leading . in from .AsyncDownloader as well. If you like, you can make the import absolute by changing it to:

from enclosing_folder.AsyncDownloader import AsyncDownloader

Upvotes: 4

Related Questions