7

I want to import a module in a project and it gives me lots of troubles because of an import error. So I decided to write a little test to see where the problem lies. I add a folder to my sys path and try to import it. And I get an Import Error: no module found named xyz

Like this:

import sys
import os

sys.path.insert(0, os.path.abspath('../../myfolder'))
import myfolder
print(sys.path)

The sys.path is ['/Users/myuser/myproject/mywebsitefolder/myfolder/', ...]

myfolder contains an __init__.py file. Hardcoding the path to myfolder has same results. Other questions on the web solve the problem by either adding the correct path or by adding an init. But I have both I think and the problem remains.

I was under the impression that python looks in the system path for importable modules or do I misunderstand how this is supposed to work?

If I understand correctly, is there any way I can debug this further? Or could this be a problem with python versions?

Help is very much appreciated. Thanks in advance!

Edit: Here is my structure of my directories

  • mywebsitefolder
    • myfolder
      • api_supply
        • tests (contains all my tests with many files)
        • init.py
        • serializers.py
        • urls.py
        • views.py
      • api_demand
        • tests (contains all my tests with many files)
        • init.py
        • serializers.py
        • urls.py
        • views.py
      • migrations (folder)
      • templates (folder)
      • init.py
      • admin.py
      • apps.py
      • models.py
      • tests.py
      • urls.py
      • views.py
Micromegas
  • 1,499
  • 2
  • 20
  • 49
  • 1
    If you're trying to import `myfolder`, it is the *parent* - ie `mywebsitefolder` - that you need to add to sys.path. – Daniel Roseman Nov 22 '19 at 08:29
  • Thanks Daniel. I also tried this. Doing ```sys.path.insert(0, os.path.abspath('/Users/myuser/myproject/mywebsitefolder/'))``` gives me the same error – Micromegas Nov 22 '19 at 08:36
  • 1
    Can you show the actual contents of mywebsitefolder and its subdirectories? – Daniel Roseman Nov 22 '19 at 08:37
  • yes of course. Updated my question. – Micromegas Nov 22 '19 at 08:49
  • 1
    I can't see the structure from that - is myfolder inside mywebsitefolder? - and I can't see what you are trying to import. But, this is clearly a Django project; so why do you need to do this at all? Your project will already be on the path, you should be able to import anything within it already. – Daniel Roseman Nov 22 '19 at 08:51
  • Yes my folder is inside mywebsitefolder. Actually you were right again... ```Doing sys.path.insert(0, os.path.abspath('/Users/myuser/myproject/mywebsitefolder/')``` does work. My mistake, I tried it in my shpinx project and not in my test script and the error changed. I was too quick checking. In my test script it does work. I get other import errors in my sphinx build. – Micromegas Nov 22 '19 at 09:05

1 Answers1

6

Replace the code as this you dont need to add the folder to the path all you need is the path to the folder

import sys
import os

sys.path.insert(0, os.path.abspath('../../'))
import myfolder
print(sys.path)
ArunJose
  • 1,999
  • 1
  • 10
  • 33
  • thanks ArunJose_intel! I also tried this, but it gives me the same error unfortunately... – Micromegas Nov 22 '19 at 08:35
  • can you try sys.path.append('/Users/myuser/myproject/mywebsitefolder/') – ArunJose Nov 22 '19 at 08:42
  • In the directory structure posted your myfolder is in the same level as mywebsitefolder so it should be sys.path.append('/Users/myuser/myproject/') – ArunJose Nov 22 '19 at 09:03
  • Thanks ArunJose_intel ```'/Users/myuser/myproject/mywebsitefolder/``` actually worked. I mixed my test script up with my sphinx build where I was trying it. I get other import errors there but this one is solved. Thanks so much for the help – Micromegas Nov 22 '19 at 09:07