1

I have this line in a Python script "lxml.py": from lxml import html. When I run the script within the conda's base environment, it returns an ImportError:

(base) $ python lxml.py
Traceback (most recent call last):
  File "lxml.py", line 2, in <module>
    from lxml import html
  File "/Users/max/Dropbox/WORK/python/projects/web_scraping/lxml.py", line 2, in <module>
    from lxml import html
ImportError: cannot import name 'html' from 'lxml' (/Users/max/Dropbox/WORK/python/projects/web_scraping/lxml.py)

However, oddly enough, if I try the command from lxml import html in a Python shell, sometimes it works just fine and imports the library, while other times it throws the same error. It seems that it stops working in a shell after I try running the script using the python command.

Edit: Okay, it's really strange. If I open the terminal and run import lxml in a Python shell, it works every time. Then, I run the script lxml.py and it throws the error. But then, here's the thing, if I run import lxml in the Python shell, it returns this:

Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/max/Dropbox/WORK/python/projects/web_scraping/lxml.py", line 2, in <module>
    from lxml import html
ImportError: cannot import name 'html' from 'lxml' (/Users/max/Dropbox/WORK/python/projects/web_scraping/lxml.py)

Somehow, it 'remembers' the script I ran before (wtf?).

When in the conda's base environment, I check whether the "lxml" package is installed (it is):

(base) $ conda list | grep "lxml"
lxml    4.5.0    pypi_0    pypi

Then I try to delete it, but can't because of a PackagesNotFoundError:

(base) $ conda remove lxml
Collecting package metadata (repodata.json): done
Solving environment: failed

PackagesNotFoundError: The following packages are missing from the target environment:
  - lxml

The /pkgs directory doesn't contain any lxml files:

(base) $ cd ~/opt/anaconda3/pkgs && ls | grep "lxml"
returns nothing

Here's the relevant content of the site-packages directory:

(base) $ cd ~/opt/anaconda3/lib/python3.7/site-packages && ls | grep "lxml"
lxml
lxml-4.5.0.dist-info

I tried installing the package in a different conda environement: same problem.

Don't know if it's relevant, my PATH variable is:

/Users/max/opt/anaconda3/bin:/Users/max/opt/anaconda3/condabin:/Users/max/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin

Also, I did this:

(base) $ which python && python --version
/Users/max/opt/anaconda3/bin/python
Python 3.7.4

Can anybody help me figure out what the heck is going on here?

m_ocean
  • 327
  • 3
  • 11
  • 1
    I think this is due to a naming conflict. The name of your script `lxml.py` conflicts with the `lxml` module. Try renaming your script e.g. `lxml.py` -> `mylxml.py` and see whether that solves your problem – cel Mar 05 '20 at 14:35
  • 1
    Damn, thank you! As easy as that... It helped! – m_ocean Mar 05 '20 at 14:42
  • Why use the base environment for this? – AMC Mar 05 '20 at 16:15
  • Does this answer your question? [importing module with same name as file](https://stackoverflow.com/questions/34000610/importing-module-with-same-name-as-file) – AMC Mar 10 '20 at 23:16

0 Answers0