4

I am facing weird problem, although new to python. And this looks bit different from already stated on several forums.

Directory structure:

Project_Folder
 -- Folder A 
 -- SubFolder A1
 -- Subfolder A2 
 -- Subfolder A3 
      -- Folder A3-1 
         -- XYZ.py 
 -- Subfolder A4 ( this contains utility classes)
       -- A4-1.py
       -- A4-2.py

NOTE: All folders contain __init__.py, also PYTHON PATH contains all required directories in PATH.

Script XYZ.py ... is dependent on below 2 utilities classes. Scipts starts with appending on sys.path the sub-folder A4 so ideally there is not need to use A4.A4-1.py instead directly A4-1 should work on import. Like below from A4-1.py import sub-methods from A4-2.py import sub-methods

But this is giving an issue ... as stated in subject. However, same works if I use A4.A4-1.py

Weird part, is same script work on server where project was already setup.

Being new to Python, I need to understand how I am able to execute this script from local machine. (without changing or using Module name in import)

Also, I am using IDE INtelliJ where I have added A4 as dependency to my project. And compiler is able to resolve it but execution is throwing import error ...

Any help is appreciated.

5
  • You can put backticks around __init__.py so that you don't get a bold, and people won't suspect that is the error. Commented Jun 9, 2014 at 12:29
  • Could you show the path and the code that adds this directory to path? Also, you shouldn't need to include .py in your import statement. Commented Jun 9, 2014 at 12:38
  • i didn't get you sorry, note, init.py is already there within folder...i think editor has removed underscores in init.py ...probably that confused you ? Commented Jun 9, 2014 at 12:38
  • import sys,os,time,datetime testdir = os.path.dirname(os.path.abspath(file)) sys.path.append(testdir+"\\..\\..\\utilities") from xyzUtil import XYZ from abcUtil import ABC Commented Jun 9, 2014 at 12:40
  • @Mark let me know if you need additional information ..Thanks !! Commented Jun 9, 2014 at 12:43

2 Answers 2

2

This is now resolved, problem was clash of similar folders on PYTHONPATH , there was a path where utilities existed but no files were there...After removing path itself, it searched on right path for util files...

Sign up to request clarification or add additional context in comments.

Comments

1

Are the files really named A4-1.py etc? Because that gives me a SyntaxError rather than an ImportError; the - sign is (apparently) not allowed in module names. Which makes sense, because it means minus.

If you are importing within the same probject, I would personally say that in most cases, importing like this

from A4.A4_1 import submethods

is better than adding A4 to your path and then importing from A4_1 directly.

EDIT

Could you try if your path works if you resolve the ..?

import sys,os,time,datetime
testdir = os.path.dirname(os.path.abspath(__file__))
newdir = os.path.abspath(os.path.join(testdir, '../../utilities'))
sys.path.append(newdir)

If not, can you verify that the correct absolute path is included using

print sys.path

EDIT2

If you're checking sys.path, also make sure there's not another directory that matches the start of the import but does not contain the rest (e.g. submethods). If there's another directory A4, maybe Python is using the wrong one.

Also make sure the name is not an existing Python module. (E.g. the first part of the import still works if you rename your module).

5 Comments

Hi, A4-1.py is just provided to explain, and avoid providing real project class file names....you can safely assume that things are working fine and naming conventions are properly followed. However, as mentioned importing using modulename.modulefile is working perfectly fine. only problem is adding path and then importing from file is not workign on client. But works on Server. Being fairly new to Python, i am not getting where is the catch. Am i missing something or i need to additionally provide something to execute this.
Adding to path (sys.path.append) should be able to work. But as suggested in the answer, I would stick with the modulename.modulefile, it seems better than changing your path just to import.
Problem is, on server where sys.path.append works, modulename.modulefile doesn;t . and on client otherway around....i need to understand what configuration is missing ( atleast on client to make it able to work ) You mentioned sys.path.append should work ...but in my case it is not...PLs suggest any reason for this ?
Not sure but I added a possibility
Path resolves utilities, more details on error: XYZ.py --> imports 2 files in utilities folder, say A4_1.py and A4_2.py. A4_1.py import A4_2.py ( but not other way around) Now Error traceback, mentions Import Error on A4_2.py in file A4_1.py That means, the file within same folder itself, is not visible ? I'm not sure wht's causing this to happen ? ( also this is not the case of cyclic dependency)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.