0

I downloaded a repository which other people are using. This file works for everyone else except for me so I believe there is a problem with my local setup.

There is a line from lib import mailparser which is causing the error:

Traceback (most recent call last):
  File "parse.py", line 3, in <module>
    from lib import mailparser
ImportError: cannot import name 'mailparser' from 'lib' (/usr/local/lib/python3.7/site-packages/lib/__init__.py)

There is 100% a file called 'mailparser' in the 'lib' directory but it isn't recognizing it.

From the error it seems to be looking in the usr/local/lib/python3.7/site-packages/lib which has to be wrong as the correct path is /Users/myname/Documents/Company Name/parser/lib/mailparser.py.

1 Answers1

0

If that's where you've downloaded the lib package to, then you'll need to do

sys.path.insert(0, "<PATH-TO-PACKAGE>")

at the top of your file to get Python to look in there for it.

The problem here is that you have a package called lib that already exists in your site-packages folder, which is where Python looks for files to import. In the case of your colleagues, they have nothing, so Python falls-back to looking in the current working directory for something called lib. In your case, it finds this random lib and uses that. To avoid that, you tell Python to look at your current working dircetory first, by inserting it into the first position of your sys.path.

If the repository you've downloaded has a setup.py file, you might be expected to go into the repo and run pip install . to install from source. This will install the code into your site-packages.

Zain Patel
  • 983
  • 5
  • 14
  • No one else has had to do this though. I'm more than sure it is a problem with my setup. – yorkshirepudding Nov 07 '19 at 16:26
  • If you downloaded the repo, is it a python package? You might need to go into it and run `pip install .` so it installs from the `setup.py` into your `site-packages`. – Zain Patel Nov 07 '19 at 16:28
  • No it is has been written by us – yorkshirepudding Nov 07 '19 at 16:31
  • Then the problem seems to be that you have already have a package called `lib` in your `site-packages` unlike your colleagues, that don't, so Python looks through the current working tree for something called `lib` and finds it, but in your case, it looks through site-packages first, sees lib and thinks that's what you mean. Either `pip uninstall lib` or do the `sys.path` trick. – Zain Patel Nov 07 '19 at 16:33