2

I have a specific problem which might require a general solution. I am currently learning apache thrift. I used this guide.I followed all the steps and i am getting a import error as Cannot import module UserManager. So the question being
How does python import lookup take place. Which directory is checked first. How does it move upwards?
How does sys.path.append('') work?

I found out the answer for this here. I followed the same steps. But i am still facing the same issue. Any ideas why? Anything more i should put up that could help debug you guys. ?

Help is appreciated.

0

5 Answers 5

6

On windows, Python looks up modules from the Lib folder in the default python path, for example from "C:\Python34\Lib\". You can add your Python libaries in a custom folder ("my-lib" or sth.) in there, but you need a file in order to tell Python that you can import from there. This file is called __init__.py , and is totally empty. That data structure should look like this:

my-lib

  • __init__.py
  • /myfolder
  • mymodule.py

    (This is how every Python module works. For example urllib.request, it's at "%PYTHONPATH%\Lib\urllib\request.py")

    You can import from the "mymodule.py" file by typing

    import my-lib
    

    and then using

    mylib.mymodule.myfunction
    

    or you can use

    from my-lib import mymodule
    

    And then just using the name of you function.

    You can now use sys.path.append to append the path you pass into the function to the folders Python looks for the modules (Please note that thats not permanent). If the path of your modules should be static, you should consider putting these in the Lib folder. If that path is relative to your file you could look for the path of the file you execute from, and then append the sys.path relative to your file, but i reccomend using relative imports.

    If you consider doing that, i recommend reading the docs, you can do that here: https://docs.python.org/3/reference/import.html#submodules

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

    2 Comments

    my folders name is gen-py and i have the init.py inside it. and my python file lies outside that folder. Here is how my python file look. import sys sys.path.append('/gen-py') even this does not work
    Python is looking is handling /gen-py as a full path. You should define the real full path, this means like "C:/Users/Saras/Desktop/python/gen-py/". You can get the path of a file like this: import os; os.path.realpath(__ file __) file of course without the blank spaces (just for editing).
    1

    If I got you right, you're using Python 3.3 from Blender but try to include the 3.2 standard library. This is bound to give you a flurry of issues, you should not do that. Find another way. It's likely that Blender offers a way to use the 3.3 standard library (and that's 99% compatible with 3.2). Pure-Python third party library can, of course, be included by fiddling with sys.path.

    The specific issue you're seeing now is likely caused by the version difference. As people have pointed out in the comments, Python 3.3 doesn't find the _tkinter extension module. Although it is present (as it works from Python 3.2), it is most likely in a .so file with an ABI tag that is incompatible with Blender's Python 3.3, hence it won't even look at it (much like a module.txt is not considered for import module). This is a good thing. Extension modules are highly version-specific, slight ABI mismatches (such as between 3.2 and 3.3, or two 3.3 compiled with different options) can cause pretty much any kind of error, from crashes to memory leaks to silent data corruption or even something completely different.

    You can verify whether this is the case via import _tkinter; print(_tkinter.file) in the 3.2 shell. Alternatively, _tkinter may live in a different directory entirely. Adding that directory won't actually fix the real issue outlined above.

    Comments

    1

    For any new readers coming along that are still having issues, try the following. This is cleaner than using sys.path.append if your app directory is structured with your .py files that contain functions for import underneath your script that imports those files. Let me illustrate.

    Script that imports files: main.py

    Function files named like: func1.py

    main.py
       /functionfolder
          __init__.py
          func1.py
          func2.py
    

    The import code in your main.py file should look as follows:

    from functionfolder import func1
    from functionfolder import func2
    

    As Agilix correctly stated, you must have an __init__.py file in your "functionfolder" (see directory illustration above).

    In addition, this solved my issue with Pylance not resolving the import, and showing me a nagging error constantly. After a rabbit-hole of sifting through GitHub issues, and trying too many comparatively complicated proposed solutions, this ever-so-simple solution worked for me.

    Comments

    0

    You may try with declaring sys.path.append('/path/to/lib/python') before including any IMPORT statements.

    Comments

    -1

    I just created a __init__.py file inside my new folder, so the directory is initialised, and it worked (:

    1 Comment

    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.