0

I am learning intra package references. I have a main package ecommerce, in which there are two sub-packages, shopping and customer. In shopping I have sales.py file, and in customer I have contact.py file.

The sales.py has

from ecommerce.customer import contact

It gives the error no module name ecommerce. But there is an app.py file in the main folder, which is not part of shopping and customer sub packages, and in this file the above import works fine.

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

1

You're probably encountering this problem because of your folder architecture.

Let's say, as you state, that you run:

from ecommerce.customer import contact

What you actually tell Python here is to import contact.py (a Python file) from the subsubfolder customer located in the subfolder named ecommerce. If you do not have a subfolder named ecommerce in your working directory, then this will not work.

Now, you can note that it probably works in your main folder because (from what I'm guessing) this is where the subfolder named ecommerce is located.


If your file is located in another directory, what you can do is adding the path to your file to the Python path at runtime:

import sys
sys.path.insert(1, '/ecommerce/shopping/customer')

# and then
import contact
bglbrt
  • 2,018
  • 8
  • 21
  • ecommerce is the folder which contain shopping and customer subfolders –  Dec 05 '19 at 10:54
  • Ok! So then what is there in the `customer` subfolder? Is there a file named `contact.py`? – bglbrt Dec 05 '19 at 10:56
  • ,yes it contains contact.py –  Dec 05 '19 at 11:00
  • Ok! And now is the Python file that you are currently working on (the one where you get an error) located in the **same** directory as the `app.py` Python file? – bglbrt Dec 05 '19 at 11:03
  • No.it is shopping directory ,which is subdirectory under ecommerce directory,in shopping directory there is sales.py file there i am getting problem. –  Dec 05 '19 at 11:10
  • I've updated my answer: please tell me if this works! – bglbrt Dec 05 '19 at 11:16
  • Sir can you tell me why normal import did not work,i am doing mosh course,in that course he directly imported,didnt use sys module,can you explain me ,why i can not do for my solution.. –  Dec 05 '19 at 11:33