Python primefac package not compiling on import

3.3k views Asked by At

I have successfully installed the python module primefac into the developer frameworks in my mac with El Capitan OS. When I invoke "import primefac", it returns this error:

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/primefac.py", line 613
    print "\033[1;31m" + (names[g] if g in names else g) + "\033[;m"
                     ^
SyntaxError: invalid syntax

I highly doubt that my version of primefac.py has a syntax error, since I replaced the file various times with primefac.py from gitHub.

I am using Python 3.5.2 with primefac-1.1. What could be the issue that causes my primefac.py to somehow be unreadable? Could it be in the wrong directory?

3

There are 3 answers

0
rdegges On

The primefac library is only compatible with Python 2 currently -- if you want to use it, you'll need to use Python 2.7 :( You're using Python 3 which means the syntax in the library won't work.

0
BPL On

If you take a look on pypi category section you'll see at this point of time primefac hasn't still being ported to python3.x. As an alternative I can suggest you can use primefac-fork which supports python3.x

0
s3c On

I am absolutely certain, that my function is no where near as optimized as their for python2, but mine works for python3.

The functions has no checker whether input is a positive integer larger than 1 or if it is anything else. You can add those in as you like. Actually I'll put an example in in a comment form.

It returns a dictionary where dict.keys are prime factors and dict.values are the exponent for respective prime factors

def primeFactors(num):
  # try:
  #   if num != int(abs(num)) or num == 1:
  #     return {"WrongInputError": num}
  # except:
  #   return {"WrongInputError": num}
  prime_factors = dict()
  i = 2
  while num > 1:
    is_factor = False
    while num % i == 0:
      if not is_factor:
        prime_factors[i] = 1
      else:
        prime_factors[i] += 1
      is_factor = True
      num //= i
    i += 1
  return prime_factors