Everyone’s code online refers to sudo apt-get #whatever#
but windows doesn’t have that feature. I heard of something called Powershell but I opened it and have no idea what it is.
I just want to get a simple environment going and lxml so I could scrape from websites.
asked Nov 20, 2015 at 4:01
5
Go to the regular command prompt and try pip install lxml
. If that doesn’t work, remove and reinstall python. You’ll get a list of check marks during installation, make sure you check pip and try pip install lxml
again afterwards.
pip
stands for pip installs packages, and it can install some useful python packages for you.
answered Nov 20, 2015 at 4:06
roflsrofls
4,8733 gold badges26 silver badges37 bronze badges
1
Try downloading the lxml
package from here, then, on the command prompt, type in:
pip install path/to/lxml_package_file
For example, after downloading lxml‑3.5.0‑cp35‑none‑win_amd64.whl
since I have Python 3.5 on a 64-bit Windows box, I type in the following commands:
cd path/to/lxml_file
pip install lxml‑3.5.0‑cp35‑none‑win_amd64.whl
Then it should install fine.
Martin Evans
44.9k16 gold badges82 silver badges93 bronze badges
answered Nov 20, 2015 at 5:33
IsxekIsxek
1,09811 silver badges19 bronze badges
Other packages may be used instead:
import xml.etree.cElementTree as etree
import xml.etree.ElementTree as etree
import cElementTree as etree
import elementtree.ElementTree as etree
The lxml.etree Tutorial
answered Feb 4, 2020 at 19:33
Davide AndreaDavide Andrea
1,3371 gold badge14 silver badges38 bronze badges
Quick Fix: Python raises the ImportError: No module named 'lxml'
when it cannot find the library lxml
. The most frequent source of this error is that you haven’t installed lxml
explicitly with pip install lxml
. Alternatively, you may have different Python versions on your computer, and lxml
is not installed for the particular version you’re using.
Problem Formulation
You’ve just learned about the awesome capabilities of the lxml
library and you want to try it out, so you start your code with the following statement:
import lxml
This is supposed to import the Pandas library into your (virtual) environment. However, it only throws the following ImportError: No module named lxml
:
>>> import lxml Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import lxml ModuleNotFoundError: No module named 'lxml'
Solution Idea 1: Install Library lxml
The most likely reason is that Python doesn’t provide lxml
in its standard library. You need to install it first!
Before being able to import the Pandas module, you need to install it using Python’s package manager pip
. Make sure pip is installed on your machine.
To fix this error, you can run the following command in your Windows shell:
$ pip install lxml
This simple command installs lxml
in your virtual environment on Windows, Linux, and MacOS. It assumes that your pip
version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):
$ python -m pip install --upgrade pip $ pip install pandas
💡 Note: Don’t copy and paste the $
symbol. This is just to illustrate that you run it in your shell/terminal/command line.
Solution Idea 2: Fix the Path
The error might persist even after you have installed the lxml
library. This likely happens because pip
is installed but doesn’t reside in the path you can use. Although pip
may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip
in the correct path.
To fix the problem with the path in Windows follow the steps given next.
Step 1: Open the folder where you installed Python by opening the command prompt and typing where python
Step 2: Once you have opened the Python
folder, browse and open the Scripts
folder and copy its location. Also verify that the folder contains the pip
file.
Step 3: Now open the Scripts
directory in the command prompt using the cd
command and the location that you copied previously.
Step 4: Now install the library using pip install lxml
command. Here’s an analogous example:
After having followed the above steps, execute our script once again. And you should get the desired output.
Other Solution Ideas
- The
ModuleNotFoundError
may appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article. - You may have mixed up Python and pip versions on your machine. In this case, to install
lxml
for Python 3, you may want to trypython3 -m pip install lxml
or evenpip3 install lxml
instead ofpip install lxml
- If you face this issue server-side, you may want to try the command
pip install --user lxml
- If you’re using Ubuntu, you may want to try this command:
sudo apt install lxml
- You can check out our in-depth guide on installing
lxml
here. - You can also check out this article to learn more about possible problems that may lead to an error when importing a library.
Understanding the “import” Statement
import lxml
In Python, the import
statement serves two main purposes:
- Search the module by its name, load it, and initialize it.
- Define a name in the local namespace within the scope of the
import
statement. This local name is then used to reference the accessed module throughout the code.
What’s the Difference Between ImportError and ModuleNotFoundError?
What’s the difference between ImportError
and ModuleNotFoundError
?
Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError
is a subclass of the ImportError
class.
You can see this in this screenshot from the docs:
You can also check this relationship using the issubclass()
built-in function:
>>> issubclass(ModuleNotFoundError, ImportError) True
Specifically, Python raises the ModuleNotFoundError
if the module (e.g., lxml
) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError
.
If an import statement cannot import a module, it raises an ImportError
. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError
.
Related Videos
The following video shows you how to resolve the ImportError
:
How to Fix : “ImportError: Cannot import name X” in Python?
The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError
:
How to Call a Function from Another File in Python?
How to Fix “ModuleNotFoundError: No module named ‘lxml’” in PyCharm
If you create a new Python project in PyCharm and try to import the lxml
library, it’ll raise the following error message:
Traceback (most recent call last): File "C:/Users/.../main.py", line 1, in <module> import lxml ModuleNotFoundError: No module named 'lxml' Process finished with exit code 1
The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed lxml
on your computer!
Here’s a screenshot exemplifying this for the pandas
library. It’ll look similar for lxml
.
The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!
First, right-click on the pandas
text in your editor:
Second, click “Show Context Actions
” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.
The code will run after your installation completes successfully.
As an alternative, you can also open the Terminal
tool at the bottom and type:
$ pip install lxml
If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html
You can also manually install a new library such as lxml
in PyCharm using the following procedure:
- Open
File > Settings > Project
from the PyCharm menu. - Select your current project.
- Click the
Python Interpreter
tab within your project tab. - Click the small
+
symbol to add a new library to the project. - Now type in the library to be installed, in your example Pandas, and click
Install Package
. - Wait for the installation to terminate and close all popup windows.
Here’s an analogous example:
Here’s a full guide on how to install a library on PyCharm.
- How to Install a Library on PyCharm
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.
Have installed required module lxml
but when imported, errors ImportError: No module named lxml
How do I know
- From which location python modules are being considered?
- Which python version and pip is in use or set ?
Please help me to understand and resolve this issue.
uname -a
Linux machine-name 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
which python
/usr/local/bin/python
which pip
/usr/local/bin/pip
pip --version
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
python
Python 2.7.16 (default, May 6 2020, 13:05:58)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import urllib2
>>> os.path.abspath(urllib2.__file__)
'/usr/local/lib/python2.7/urllib2.pyc'
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
echo $PYTHONPATH
doesn't display anything (empty)
ls -ls /usr/bin/python*
0 lrwxrwxrwx 1 root root 9 /usr/bin/python -> python2.7
0 lrwxrwxrwx 1 root root 16 /usr/bin/python-config -> python2.7-config
0 lrwxrwxrwx 1 root root 9 /usr/bin/python2 -> python2.7
0 lrwxrwxrwx 1 root root 16 /usr/bin/python2-config -> python2.7-config
3264 -rwxr-xr-x 1 root root 3341288 /usr/bin/python2.7
0 lrwxrwxrwx 1 root root 33 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
0 lrwxrwxrwx 1 root root 9 /usr/bin/python3 -> python3.4
3628 -rwxr-xr-x 2 root root 3714088 /usr/bin/python3.4
3628 -rwxr-xr-x 2 root root 3714088 /usr/bin/python3.4m
0 lrwxrwxrwx 1 root root 10 /usr/bin/python3m -> python3.4m
ls -ls /usr/local/bin/python*
0 lrwxrwxrwx 1 root root 9 /usr/local/bin/python -> python2.7
0 lrwxrwxrwx 1 root root 14 /usr/local/bin/python-config -> python2-config
0 lrwxrwxrwx 1 root root 7 /usr/local/bin/python.orig -> python2
0 lrwxrwxrwx 1 root root 9 /usr/local/bin/python2 -> python2.7
0 lrwxrwxrwx 1 root root 16 /usr/local/bin/python2-config -> python2.7-config
8280 -rwxr-xr-x 1 root root 8478000 /usr/local/bin/python2.7
4 -rwxr-xr-x 1 root root 1687 /usr/local/bin/python2.7-config
find /bin /usr /lib* /home -type f -name "pip*" -executable
/usr/local/bin/pip
/usr/local/bin/pip2.7
/usr/local/bin/pip2
/usr/bin/pip
/usr/bin/pip2
/usr/bin/pip install lxml
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/dist-packages
Cleaning up...
/usr/bin/pip2 install lxml
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/dist-packages
Cleaning up...
/usr/local/bin/pip2 install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)
/usr/local/bin/pip2.7 install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)
/usr/local/bin/pip install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)
sudo pip install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
The directory '/home/abcd/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/abcd/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)
python -m pip install lxml
/usr/local/bin/python: No module named pip
python
Python 2.7.16 (default, May 6 2020, 13:05:58)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named lxml
import sys
print("n".join(sys.path))
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
Apr-15-2018, 09:17 PM
(This post was last modified: Apr-15-2018, 09:42 PM by DiceMann.)
Hello all!
On Windows 10, running Python 3.6.5 (v3.6.5:f59c0932b4, after following exactly snippsat installation here:
https://python-forum.io/Thread-Basic-Par…8#pid18498
I am unable to install the «Requests» module.
I can run PIP in windows, and can run python from the cmd line, but i cant
Whats crazy is that i CAN import modules like statistics.
Im looking for any help i can find with this.
C:UsersMYNAME>pip install requests
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2018.1.18-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Installing collected packages: idna, certifi, chardet, urllib3, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
The C prompt never re-appeared after installation.
I had to actually and completley exit out of dos prompt/CMD and re open it.
import requests Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> import requests ModuleNotFoundError: No module named 'requests'
>>>
(Apr-15-2018, 09:17 PM)DiceMann Wrote: Hello all!
On Windows 10, running Python 3.6.5 (v3.6.5:f59c0932b4, after following exactly snippsat installation here:
https://python-forum.io/Thread-Basic-Par…8#pid18498
I am unable to install the «Requests» module.
I can run PIP in windows, and can run python from the cmd line, but i cant
Whats crazy is that i CAN import modules like statistics.
Im looking for any help i can find with this.C:UsersMYNAME>pip install requests
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2018.1.18-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Installing collected packages: idna, certifi, chardet, urllib3, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22The C prompt never re-appeared after installation.
I had to actually and completley exit out of dos prompt/CMD and re open it.
import requests Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> import requests ModuleNotFoundError: No module named 'requests'>>>
So..i realized i needed this too lxml.
Problem is when i run it..its imply hangs….
C:Python36Scripts>pip install lxml
Collecting lxml
Has this always been necessary with the requests module?
Posts: 6,567
Threads: 116
Joined: Sep 2016
Reputation:
487
Apr-15-2018, 10:16 PM
(This post was last modified: Apr-15-2018, 10:16 PM by snippsat.)
Look at part-1 and the test in cmd.
You see how Requests is installed and tester.
(Apr-15-2018, 09:17 PM)DiceMann Wrote: Has this always been necessary with the requests module?
No Requests has always work alone,you most be sure that test of pip
and python
work in cmd as part-1.
Microsoft Windows [Version 10.0.16299.309] (c) 2017 Microsoft Corporation. Med enerett. C:WindowsSystem32>cd C:>pip -V pip 9.0.3 from c:python36libsite-packages (python 3.6) C:>python -c "import sys; print(sys.executable)" C:python36python.exe C:>
Posts: 7
Threads: 2
Joined: Apr 2018
Reputation:
0
Apr-16-2018, 01:44 AM
(This post was last modified: Apr-16-2018, 01:44 AM by DiceMann.)
So I have:
C:>pip install requests
Requirement already satisfied: requests in c:python36libsite-packages (2.18.4)
Requirement already satisfied: idna<2.7,>=2.5 in c:python36libsite-packages (from requests) (2.6)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:python36libsite-packages (from requests) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:python36libsite-packages (from requests) (2018.1.18)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:python36libsite-packages (from requests) (1.22)
I thought I had all bases covered.
Also
C:WINDOWSsystem32>python -c «import sys; print(sys.executable)»
C:Python36python.exe
C:WINDOWSsystem32>
I also heard you have to install «lmxl» but when i do it does this:
C:WINDOWSsystem32>pip install lxml
Collecting lxml
Problem is, it sits there and hangs for hours. Doesnt complete.
Thanks!
ps.
C:>python -V
Python 3.6.5
Posts: 69
Threads: 34
Joined: Sep 2017
Reputation:
0
Apr-16-2018, 08:07 AM
(This post was last modified: Apr-16-2018, 08:07 AM by digitalmatic7.)
I think with Windows you can’t install lxml through pip
You need to download the whl file from the website and run it through CMD
Posts: 6,567
Threads: 116
Joined: Sep 2016
Reputation:
487
Apr-16-2018, 11:26 AM
(This post was last modified: Apr-16-2018, 11:26 AM by snippsat.)
(Apr-16-2018, 01:44 AM)DiceMann Wrote: Problem is, it sits there and hangs for hours. Doesnt complete.
What hangs when you install or run code?
Run cmd as Administrator
Your python
and pip
is correct from cmd.
(Apr-16-2018, 08:07 AM)digitalmatic7 Wrote: I think with Windows you can’t install lxml through pip
You need to download the whl file from the website and run it through CMD
With pip-9 and 3.6 there is no need to download the wheel file,pip install lxml
work fine.
C:>pip -V pip 9.0.3 from c:python36libsite-packages (python 3.6) C:>pip install lxml Collecting lxml Using cached lxml-4.2.1-cp36-cp36m-win32.whl Installing collected packages: lxml Successfully installed lxml-4.2.1 C:>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from lxml import html, etree >>> etree.__version__ '4.2.1'
Posts: 7
Threads: 2
Joined: Apr 2018
Reputation:
0
Update.
So i can install requests completely:
C:>pip install requests
Requirement already satisfied: requests in c:pythonlibsite-packages (2.18.4)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:pythonlibsite-packages (from requests) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:pythonlibsite-packages (from requests) (2018.4.16)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:pythonlibsite-packages (from requests) (1.22)
Requirement already satisfied: idna<2.7,>=2.5 in c:pythonlibsite-packages (from requests) (2.6)
So i think i resolved an issue there, but when i run requests for python im back to the main issue:
>>> import requests
Traceback (most recent call last):
File «<stdin>», line 1, in <module>
ModuleNotFoundError: No module named ‘requests’
>>>
For lxml:
C:>pip install lxml
Collecting lxml
Downloading https://files.pythonhosted.org/packages/…_amd64.whl (3.6MB)
100% |████████████████████████████████| 3.6MB 924kB/s
Installing collected packages: lxml
Successfully installed lxml-4.2.1
When i run it for import:
>>> import lxml
Traceback (most recent call last):
File «<stdin>», line 1, in <module>
ModuleNotFoundError: No module named ‘lxml’
>>>
>>> from lxml import html, etree
Traceback (most recent call last):
File «<stdin>», line 1, in <module>
ModuleNotFoundError: No module named ‘lxml’
>>>
Really confused here.
Thanks
Posts: 6,567
Threads: 116
Joined: Sep 2016
Reputation:
487
(Apr-17-2018, 02:16 AM)DiceMann Wrote: Requirement already satisfied: requests in c:pythonlibsite-packages (2.18.4)
Why dos it say c:python
libsite-packages
In your previous post:
DiceMann Wrote:Requirement already satisfied: requests in c:python36libsite-packages (2.18.4)
There it say c:python36
libsite-packages
Do you have two Python installation?
You have to figure this out,what python and pip version used anywhere from cmd
is set in Environment Variables(Path)
Posts: 7
Threads: 2
Joined: Apr 2018
Reputation:
0
snippsat,
I really appreciate and am thankful for your help!!
So..i ended up uninstalling and re installing python again.
I just re ran the command to show that it was, in fact, installed.
Im not really sure if i have two python installations or not.
I thought i had thoroughly uninstalled python but i did notice that the uninstaller doesnt uninstall every thing.
C:>python -V
Python 3.6.5
and
C:>pip -V
pip 10.0.0 from c:pythonlibsite-packagespip (python 3.6)
I did not see the option for «Disable Path Length Limit» during the installation.
ADD PYTHON 3.6 TO PATH was selected during installation.
Posts: 7
Threads: 2
Joined: Apr 2018
Reputation:
0
The only other thing i can think of is a fresh install of windows.
Otherwise, none of this makes since.
wavic
So-and-so of the Yard
Posts: 2,955
Threads: 48
Joined: Sep 2016
Reputation:
90
Upgrade pip to v. 10 and try again. There was some improvements for Windows.