LookIntoEast
LookIntoEast

Reputation: 8798

Import result from another script in python

I have a supporting library

##lib.py
def bagwords(X):
    XXX
    return data

Now in my abc.py I'd like to use lib.py and returned data

##abc.py
import lib
lib.bagwords(X)

I'm wondering in my abc.py, how can I pass variable to lib.py and use return data?

Upvotes: 0

Views: 1339

Answers (2)

user7711283
user7711283

Reputation:

##abc.py
import lib

data = lib.bagwords(X)

This above will throw an error because X is not defined, but at least it code demonstrates how to get data from the lib.bagwords() function.

OK - from the comment ("No, X is defined in abc.py, my problem is: it'll have error: module lib has no attribute bagwords, i don't know why.") is known that X is defined in abc.py, so there will be no error because of that

One possible reason for the error mentioned in the comment is if there is ANOTHER module named lib in the search path and it is found and loaded first. So how to avoid the error?

Rename lib.py to myLibrary.py and use the code:

##abc.py
import myLibrary

data = myLibrary.bagwords(X)

I have tested that it works, so it should work also for you :) . Happy coding!

Upvotes: 1

mepi0011
mepi0011

Reputation: 55

are lib.py and abc.py in the same directory then create the following empty python file __ init __.py to allow import from this directory.

Change your abc.py file as follow:

##abc.py
from lib import bagwords
lib.bagwords(X)

PS: please have a look at Python: import module from another directory at the same level in project hierarchy

Upvotes: 0

Related Questions