Zanam
Zanam

Reputation: 4807

Using packages by providing sys path

I came across this link:

(Python) Use a library locally instead of installing it

And followed the steps to provide path to IBPy package which I downloaded and extracted on my desktop.

My code looks like:

import sys
sys.path.append('C:\Users\Duck\Desktop\IbPy-0.7.6-9.51\build\lib\ib')
import IbPy

But the error on third line is: No module named IbPY. I tried different variants on the path for IbPy as: C:\Users\Duck\Desktop\IbPy-0.7.6-9.51

Any suggestions ? My problem is I can't install packages even locally. Only thing I am allowed to do is bring packages through USB.

Edit:

(1) Yes there is init.py file

(2) I am now using

sys.path.append(r'C:\Users\Duck\Desktop\IbPy-0.7.6-9.51\build\lib\ib')

The error is still there.

I am using Pycharm as my editor and I downloaded the IBPy from:

https://github.com/blampe/IbPy

Upvotes: 3

Views: 483

Answers (2)

tdelaney
tdelaney

Reputation: 77407

The package is the shallowest directory with an __init__.py and that directory name is the package name. sys.path needs to include the directory before the package directory because python will append the package name to the names in sys.path until it finds a match. Since the directory ib holds the package __init__.py you need to

import sys
sys.path.append(r'C:\Users\Duck\Desktop\IbPy-0.7.6-9.51\build\lib')
import ib

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155674

Use raw strings for Windows paths. The \b is being interpreted as a backspace character.

r'C:\Users\Duck\Desktop\IbPy-0.7.6-9.51\build\lib\ib'

should work. Raw strings prevent the interpretation of backslash escapes for all but the string's quote character.

Upvotes: 3

Related Questions