{"id":126307,"date":"2022-07-06T15:01:30","date_gmt":"2022-07-06T15:01:30","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=457044"},"modified":"2022-07-06T15:01:30","modified_gmt":"2022-07-06T15:01:30","slug":"how-to-install-psycopg2-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/06\/how-to-install-psycopg2-in-python\/","title":{"rendered":"How to Install psycopg2 in Python?"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;457044&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install psycopg2<\/pre>\n<p>The Python <strong>psycopg2 <\/strong>library is among the top 100 Python libraries, with more than <strong>15,749,750<\/strong> downloads. This article will show you everything you need to get this installed in your Python environment.<\/p>\n<ul>\n<li><a href=\"https:\/\/pypi.org\/project\/psycopg2\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/pypi.org\/project\/psycopg2\">Library Link<\/a><\/li>\n<\/ul>\n<h2>How to Install psycopg2 on Windows?<\/h2>\n<ol>\n<li>Type <code>\"cmd\"<\/code> in the search bar and hit <code>Enter<\/code> to open the command line.<\/li>\n<li>Type &#8220;<code>pip install psycopg2<\/code>&#8221; (without quotes) in the command line and hit <code>Enter<\/code> again. This installs psycopg2 for your default Python installation.<\/li>\n<li>The previous command may not work if you have both Python versions 2 and 3 on your computer. In this case, try <code>\"pip3 install psycopg2\"<\/code> or &#8220;<code>python -m pip install psycopg2<\/code>&#8220;.<\/li>\n<li>Wait for the installation to terminate successfully. It is now installed on your Windows machine.<\/li>\n<\/ol>\n<p>Here&#8217;s how to open the command line on a (German) Windows machine:<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"986\" height=\"852\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/09\/image-96.png\" alt=\"Open CMD in Windows\" class=\"wp-image-35703\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/09\/image-96.png 986w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/09\/image-96-300x259.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/09\/image-96-768x664.png 768w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/><\/figure>\n<p>First, try the following command to install <strong>psycopg2<\/strong> on your system:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install psycopg2<\/pre>\n<p>Second, if this leads to an error message, try this command to install <strong>psycopg2<\/strong> on your system:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip3 install psycopg2<\/pre>\n<p>Third, if both do not work, use the following long-form command:<\/p>\n<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 psycopg2<\/pre>\n<p>The difference between <code>pip<\/code> and <code>pip3<\/code> is that <code>pip3<\/code> is an updated version of <code>pip<\/code> for Python <a href=\"https:\/\/blog.finxter.com\/how-to-check-your-python-version\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"HOW TO CHECK YOUR PYTHON VERSION\">version <\/a>3. Depending on what&#8217;s first in the <code>PATH<\/code> variable, <code>pip<\/code> will refer to your Python 2 or Python 3 installation&#8212;and you cannot know which without checking the environment variables. To resolve this uncertainty, you can use <code>pip3<\/code>, which will always refer to your default Python 3 installation.<\/p>\n<h2>How to Install psycopg2 on Linux?<\/h2>\n<p>You can install psycopg2 on Linux in four steps:<\/p>\n<ol>\n<li>Open your Linux terminal or shell<\/li>\n<li>Type &#8220;<code>pip install psycopg2<\/code>&#8221; (without quotes), hit Enter.<\/li>\n<li>If it doesn&#8217;t work, try <code>\"pip3 install psycopg2\"<\/code> or &#8220;<code>python -m pip install psycopg2<\/code>&#8220;.<\/li>\n<li>Wait for the installation to terminate successfully. <\/li>\n<\/ol>\n<p>The package is now installed on your Linux operating system.<\/p>\n<h2>How to Install psycopg2 on macOS?<\/h2>\n<p>Similarly, you can install psycopg2 on macOS in four steps:<\/p>\n<ol>\n<li>Open your macOS terminal.<\/li>\n<li>Type &#8220;<code>pip install psycopg2<\/code>&#8221; without quotes and hit <code>Enter<\/code>.<\/li>\n<li>If it doesn&#8217;t work, try <code>\"pip3 install psycopg2\"<\/code> or &#8220;<code>python -m pip install psycopg2<\/code>&#8220;.<\/li>\n<li>Wait for the installation to terminate successfully. <\/li>\n<\/ol>\n<p>The package is now installed on your macOS.<\/p>\n<h2>How to Install psycopg2 in PyCharm?<\/h2>\n<p>Given a <a href=\"https:\/\/blog.finxter.com\/pycharm-a-simple-illustrated-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\">PyCharm <\/a>project. How to install the psycopg2 library in your project within a <a href=\"https:\/\/blog.finxter.com\/python-virtual-environments-with-venv-a-step-by-step-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\">virtual environment<\/a> or globally? Here\u2019s a <a href=\"https:\/\/blog.finxter.com\/how-to-install-a-library-on-pycharm\/\" target=\"_blank\" rel=\"noreferrer noopener\">solution <\/a>that always works:<\/p>\n<ul>\n<li>Open <code><strong>File &gt; Settings &gt; Project<\/strong><\/code> from the PyCharm menu.<\/li>\n<li>Select your current project.<\/li>\n<li>Click the <code><strong>Python Interpreter<\/strong><\/code> tab within your project tab.<\/li>\n<li>Click the small <code><strong>+<\/strong><\/code> symbol to add a new library to the project.<\/li>\n<li>Now type in the library to be installed, in your example <code>\"psycopg2\"<\/code> without quotes, and click <code><strong>Install Package<\/strong><\/code>.<\/li>\n<li>Wait for the installation to terminate and close all pop-ups.<\/li>\n<\/ul>\n<p>Here\u2019s the general package installation process as a short animated video\u2014it works analogously for psycopg2 if you type in <em>&#8220;psycopg2&#8221;<\/em> in the search field instead:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/media.giphy.com\/media\/VQoZIvOyP23tVvQlW0\/source.gif\" alt=\"\"\/><\/figure>\n<p>Make sure to select only <em>&#8220;psycopg2&#8221; <\/em>because there may be other packages that are not required but also contain the same term (<em>false positives<\/em>):<\/p>\n<h2>How to Install psycopg2 in a Jupyter Notebook?<\/h2>\n<p>To install any package in a Jupyter notebook, you can prefix the <code>!pip install my_package<\/code> statement with the exclamation mark <code>\"!\"<\/code>. This works for the psycopg2 library too:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">!pip install my_package<\/pre>\n<p>This automatically installs the psycopg2 library when the cell is first executed.<\/p>\n<h2>How to Resolve ModuleNotFoundError: No module named &#8216;psycopg2&#8217;?<\/h2>\n<p>Say you try to import the psycopg2 package into your Python script without installing it first:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import psycopg2\n# ... ModuleNotFoundError: No module named 'psycopg2'<\/pre>\n<p>Because you haven&#8217;t installed the package, Python raises a <code>ModuleNotFoundError: No module named 'psycopg2'<\/code>. <\/p>\n<p>To fix the error, install the psycopg2 library using &#8220;<code>pip install psycopg2<\/code>&#8221; or &#8220;<code>pip3 install psycopg2<\/code>&#8221; in your operating system&#8217;s shell or terminal first. <\/p>\n<p>See above for the different ways to install psycopg2 in your environment.<\/p>\n<h2>Improve Your Python Skills<\/h2>\n<p>If you want to keep improving your Python skills and learn about new and exciting technologies such as Blockchain development, machine learning, and data science, check out the Finxter <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" title=\"Email Academy\" target=\"_blank\" rel=\"noreferrer noopener\">free email academy<\/a> with cheat sheets, regular tutorials, and programming puzzles.<\/p>\n<p>Join us, it&#8217;s fun! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) pip install psycopg2 The Python psycopg2 library is among the top 100 Python libraries, with more than 15,749,750 downloads. This article will show you everything you need to get this installed in your Python environment. Library Link How to Install psycopg2 on Windows? Type &#8220;cmd&#8221; in the search bar and hit [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126307","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126307"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126307\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}