Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm]

#1
How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm]

<div><p class="has-pale-cyan-blue-background-color has-background"><strong>Quick Fix:</strong> Python throws the <em>“ImportError: No module named pandas”</em> when it cannot find the Pandas installation. The most frequent source of this error is that you haven’t installed Pandas explicitly with <code>pip install pandas</code>. Alternatively, you may have different Python versions on your computer and Pandas is not installed for the particular version you’re using. To fix it, run <code>pip install pandas</code> in your Linux/MacOS/Windows terminal. </p>
<p><strong>Problem</strong>: You’ve just learned about the awesome capabilities of the Pandas library and you want to try it out, so you start with the following import statement you found on the web:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd</pre>
<p>This is supposed to import the Pandas library into your <a href="https://blog.finxter.com/python-virtual-environments-with-venv-a-step-by-step-guide/" title="Python Virtual Environments with “venv” — A Step-By-Step Guide" target="_blank" rel="noreferrer noopener">(virtual) environment</a>. However, it only throws the following import error: no module named pandas!</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import pandas as pd
ImportError: No module named pandas on line 1 in main.py</pre>
<p>You can reproduce this error in the following interactive Python shell:</p>
<p> <iframe src="https://trinket.io/embed/python/0fccab1ed2" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><strong><em>Why did this error occur? </em></strong></p>
<p>The reason is that Python doesn’t provide Pandas in its standard library. You need to install Python first!</p>
<p>Before being able to import the Pandas module, you need to install it using Python’s package manager <code data-enlighter-language="generic" class="EnlighterJSRAW">pip</code>. You can run the following command in your Windows shell:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pip install pandas</pre>
<p>Here’s the screenshot on my Windows machine:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="366" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-9-1024x366.png" alt="" class="wp-image-16576" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-9-1024x366.png 1024w, https://blog.finxter.com/wp-content/uplo...00x107.png 300w, https://blog.finxter.com/wp-content/uplo...68x274.png 768w, https://blog.finxter.com/wp-content/uplo...150x54.png 150w, https://blog.finxter.com/wp-content/uplo...mage-9.png 1100w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>This simple command installs Pandas in your virtual environment on Windows, Linux, and MacOS. It assumes that you know that your pip version is updated. If it isn’t, use the following two commands (there’s no harm in doing it anyways):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ python -m pip install --upgrade pip
...
$ pip install pandas</pre>
<p>Here’s how this plays out on my Windows command line:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="175" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-10-1024x175.png" alt="" class="wp-image-16577" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-10-1024x175.png 1024w, https://blog.finxter.com/wp-content/uplo...300x51.png 300w, https://blog.finxter.com/wp-content/uplo...68x131.png 768w, https://blog.finxter.com/wp-content/uplo...150x26.png 150w, https://blog.finxter.com/wp-content/uplo...age-10.png 1103w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>The warning message disappeared!</p>
<p>If you need to refresh your Pandas skills, check out the following Pandas cheat sheets—I’ve compiled the best 5 in this article.</p>
<p><strong>Related article:</strong> <a href="https://blog.finxter.com/pandas-cheat-sheets/" title="[PDF Collection] 7 Beautiful Pandas Cheat Sheets — Post Them to Your Wall">Top 5 Pandas Cheat Sheets</a></p>
<h2>How to Fix “ImportError: No module named pandas” in PyCharm</h2>
<p>If you create a new Python project in <a href="https://blog.finxter.com/pycharm-a-simple-illustrated-guide/" target="_blank" rel="noreferrer noopener" title="PyCharm – A Simple Illustrated Guide">PyCharm </a>and try to import the Pandas library, it’ll throw the following error:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Traceback (most recent call last): File "C:/Users/xcent/Desktop/Finxter/Books/book_dash/pythonProject/main.py", line 1, in &lt;module> import pandas as pd
ModuleNotFoundError: No module named 'pandas' Process finished with exit code 1</pre>
<p>The reason is that each PyCharm project, per default, creates a <a href="https://blog.finxter.com/python-virtual-environments-conda/" title="Python Virtual Environments with Conda — Why the Buzz?" target="_blank" rel="noreferrer noopener">virtual environment</a> in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed Pandas on your computer!</p>
<p>Here’s a screenshot:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="653" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-12-1024x653.png" alt="" class="wp-image-16587" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-12-1024x653.png 1024w, https://blog.finxter.com/wp-content/uplo...00x191.png 300w, https://blog.finxter.com/wp-content/uplo...68x490.png 768w, https://blog.finxter.com/wp-content/uplo...150x96.png 150w, https://blog.finxter.com/wp-content/uplo...age-12.png 1108w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!</p>
<p>First, right-click on the <code>pandas</code> text in your editor:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="656" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-14-1024x656.png" alt="" class="wp-image-16593" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-14-1024x656.png 1024w, https://blog.finxter.com/wp-content/uplo...00x192.png 300w, https://blog.finxter.com/wp-content/uplo...68x492.png 768w, https://blog.finxter.com/wp-content/uplo...150x96.png 150w, https://blog.finxter.com/wp-content/uplo...age-14.png 1102w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Second, click “<code>Show Context Actions</code>” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation. </p>
<p>The code will run after your installation completes successfully. </p>
<p>Here’s a complete introduction to PyCharm:</p>
<p><strong>Related Article:</strong><a href="https://blog.finxter.com/pycharm-a-simple-illustrated-guide/" target="_blank" rel="noreferrer noopener" title="PyCharm – A Simple Illustrated Guide"> PyCharm—A Helpful Illustrated Guide</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-fix-importerror-no-module-named-pandas/" target="_blank" rel="noopener noreferrer">How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm]</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/11/...s-pycharm/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016