OlivierBinette / StringCompare

Efficient String Comparison Functions and Fuzzy String Matching
https://olivierbinette.github.io/StringCompare/
17 stars 2 forks source link

[BUG] Importing(using ".", ".."). Unable to access preprocessing directory #20

Closed JerryXin2 closed 2 years ago

JerryXin2 commented 2 years ago

Describe the bug

When we try to import packages in Jaccard, we get "Import Error: attempted relative import with no known parent package". We specifically have issues with "from .comparator import StringComparator from ..preprocessing.tokenizer import Tokenizer, WhitespaceTokenizer" and the dots before comparator and preprocessing.

When we remove the dots the code runs but with the dots there the code doesn't run.

Here is our code so far:

from .comparator import StringComparator from ..preprocessing tokenizer import Tokenizer, WhitespaceTokenizer ` def jaccard(s, t, token): s_token = token(s) t_token = token(t)

intersection = [value for value in s_token if value in t_token]
len_intersection = len(intersection)
return len_intersection / (len(s_token) + len(t_token) - len_intersection)

class Jaccard(StringComparator): def init(self, similarity = True, tokenizer = WhitespaceTokenizer()): self.similarity = similarity self.tokenizer = tokenizer

def compare(self, s, t):
    if self.similarity:
        return jaccard(s, t, self.tokenizer)
    else:
        return 1 - jaccard(s, t, self.tokenizer)

' We think that the issue has to do with relative paths somehow, as .comparator gives us the error above, but comparator gives us no errors. We could not get ..preprocessing to import unless we brought tokenizer into our local directory and imported that file directly, but obviously this doesn't solve the problem. We tried on both mac and linux systems using PyCharm and a cloud-based service called repl.it.

OlivierBinette commented 2 years ago

@JerryXin2 This might have to do with executing a script versus installing the package. Can you try running the following code?

git clone https://github.com/OlivierBinette/StringCompare.git
pip install --force-reinstall -e StringCompare/
python -c "import stringcompare"

If you don't see any error, this means that the package was installed properly (in editable mode).

You can then test running code from the package by launching a Python interpreter and then calling your function:

python
>>> import strincompare
>>> strincompare.mymodule.myfuntion("test", "something")

If you make changes to the cod,e then you need to quit and restart your Python interpreter for the package changes to come into effect.

Note

The above workflow can be a bit annoying at times. I'd recommend working in Python notebooks or Python scripts at first, before incorporating your code into the package.

Let me know if that solves your issue.