3

I have the following relevant libraries and files in my project:

Project_Library
    data
        some_folder
            sub_folder1
                script1.py
            sub_folder2
                script2.py

    scripts
        __init__.py
        lib_a.py

The code in script1.py works perfectly and looks a bit like this:

# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "C:/Project_Library/scripts") #


from lib_a import *
#Some more irrelevant code here

The code in script2.py doesn't work, Python claims ImportError: No Module named lib_a while I actually copied and pasted the heading of script1.py:

# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "C:/Project_Library/scripts") #

print sys.path #looks good

from lib_a import * #Crashes here
#again, more code

What possible reason can cause this?

EDIT:

A very interesting note. If I try to run script2.py after copying the file into sub_folder1 without changing a thing, it DOES work. Then why doesn't it work in sub_folder2?

EDIT2:

I did get it to work, for some reason just creating a new file and copying and pasting the file's content into it just worked miraculously! I still kept the original file with the original file name, as it still DOESN'T work on it, very interesting. I wonder what the source of the problem could possibly be

Jim
  • 125
  • 1
  • 8
  • Where is lib_a ? Check your PYTHONPATH variable – Sam Daniel Jun 01 '16 at 10:04
  • You can try to look at [this](http://stackoverflow.com/questions/31291608/effect-of-using-sys-path-insert0-path-and-sys-pathappend-when-loading-modul) post (Anand S Kumar's answe): – Lixons Jun 01 '16 at 10:05
  • What is the content of your `__init__.py` ? – 3kt Jun 01 '16 at 10:05
  • @3kt just `__all__ = ['lib_a']`. But does it even matter? I don't import scripts as a package, I add it as a folder to my path variable. – Jim Jun 01 '16 at 10:09
  • How are you executing these files? – Tadhg McDonald-Jensen Jun 01 '16 at 16:17
  • @TadhgMcDonald-Jensen Simply `python file_name.py` from the command line (with the working directory the same as the file's location) – Jim Jun 01 '16 at 16:38
  • That rules out metadata of the file being responsible for how it is executed... and if you do something like `with open("script1.py") as sc1, open("script2.py") as sc2: print(sc1.read() == sc2.read())` it shows `True` but you get different results when executing them? I'm wondering if there is a hidden character in the path that didn't copy over or something... – Tadhg McDonald-Jensen Jun 01 '16 at 16:47
  • I guess it is only a formatting problem, but your directory structure shows the library in `"C:/Project_Library/scripts"`. – mkiever Jun 01 '16 at 17:02
  • @mkiever Yeah, it was by mistake. – Jim Jun 01 '16 at 17:41
  • @TadhgMcDonald-Jensen yes, it outputs `True`. This is really odd, I have to say – Jim Jun 01 '16 at 17:41

0 Answers0