Sys.path.insert inserts path to module but imports are not working
Asked Answered
S

1

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
Spohr answered 22/11, 2019 at 8:21 Comment(6)
If you're trying to import myfolder, it is the parent - ie mywebsitefolder - that you need to add to sys.path.Vocalic
Thanks Daniel. I also tried this. Doing sys.path.insert(0, os.path.abspath('/Users/myuser/myproject/mywebsitefolder/')) gives me the same errorSpohr
Can you show the actual contents of mywebsitefolder and its subdirectories?Vocalic
yes of course. Updated my question.Spohr
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.Vocalic
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.Spohr
W
7

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)
Wove answered 22/11, 2019 at 8:30 Comment(4)
thanks ArunJose_intel! I also tried this, but it gives me the same error unfortunately...Spohr
can you try sys.path.append('/Users/myuser/myproject/mywebsitefolder/')Wove
In the directory structure posted your myfolder is in the same level as mywebsitefolder so it should be sys.path.append('/Users/myuser/myproject/')Wove
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 helpSpohr

© 2022 - 2024 — McMap. All rights reserved.