0

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?

3
  • Your bagwords function doesn't accept any parameters. Commented Apr 25, 2017 at 6:02
  • No it does accept parameters, only my typo Commented Apr 25, 2017 at 6:03
  • Aside from a second typo (bagwords vs badwords), it seems that you know how to do this. All the parts are in place. Commented Apr 25, 2017 at 6:04

2 Answers 2

1
##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!

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

2 Comments

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
Horribly sorry forget to respond!
0

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

4 Comments

I tried this, but it said ImportError: cannot import name 'bagwords'
do you have created the __ init __.py file? If lib.py is in another path you have to use from <path>/lib import bagwords.
No! I think this is problem. But how should I create? Just empty file only with name?
In my test the library is loaded from current directory without any ____init____.py file.

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.