arpit joshi arpit joshi - 1 year ago 74
Python Question

Running python from command line gives import error

I want to run my python program from command line but it gives me below error

ImportError: No module named 'main'

My folder structure is as below






My code is as below

import sys
from S3Operations import Models
if __name__ == '__main__':

And my code is

import os.path
from main import BusinessService

class ModelsMlS3(object):

def test(self):
print("Testing success")

When I run the program using command line i get the below error

$ python
Traceback (most recent call last):
File "", line 2, in <module>
from S3Operations import ModelsMlS3
File "D:\code\Project\main\", line 11, in <module>
from main import BusinessService
ImportError: No module named 'main'

Can any one please suggest a solution for the same.

Answer Source

You just need to do:

import BusinessService  # instead of `from main import BusinessService`

as in your project, there is no file present in Project directory (which holds

For importing it like:

from main import BusinessService

you need to create in the folder in order to make it as module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download