Forum in READ ONLY mode! All questions and discussions on Discord official server, invite link: https://discord.gg/VxsGzJ7

[Python] module

Ask for help
Post Reply
Roni2
Posts: 9
Joined: 10.05.2014 20:28

[Python] module

Post by Roni2 »

Hi,

I dont know why, but i cant import a library that is in the same directory of my script.
Example:

my script is in StealthDir\Script
and the library is located in the same directory.

StealthDir\Script\testLib.py
StealthDir\Script\test.py

test.py:
import testLib

The Error:
ImportError: No module named testLib

I know it work, because a friend mine runs the same instruction and worked fine, seems to be a configuration problem on my pc.

anyone got this problem?

thanks
User avatar
Vizit0r
Developer
Developer
Posts: 3958
Joined: 24.03.2005 17:05
Contact:

Re: [Python] module

Post by Vizit0r »

i can't help - i dont use Py.

Python user invited in thread :)
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Re: [Python] module

Post by Boydon »

This is strange, maybe it is related to Python version.
With Python 3.x the import logic has been changed, try with

Code: Select all

import .testLib.py #notice the dot before
Roni2
Posts: 9
Joined: 10.05.2014 20:28

Re: [Python] module

Post by Roni2 »

I`m using version 2.7.8.
Using windows 8.1 64bit, can be a problem too
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Re: [Python] module

Post by Boydon »

Then, long story short, you can (one of the following):
  • add StealthDir\Script to your PYTHONPATH (search google on how to)
  • add the following snippet at the very beginning of your test.py (before importing testLib)

    Code: Select all

    import os
    import sys
    sys.path.append(os.path.dirname(__file__))
    
    import testLib #after you modified system.path
    
Roni2
Posts: 9
Joined: 10.05.2014 20:28

Re: [Python] module

Post by Roni2 »

The pythonpath tip worked fine...
Thanks a lot
Post Reply