<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kamran Badirov</title>
    <description>The latest articles on DEV Community by Kamran Badirov (@kamrandb).</description>
    <link>https://dev.to/kamrandb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F920440%2Fca9cf898-b56b-4fa8-8421-267d870b7f44.jpeg</url>
      <title>DEV Community: Kamran Badirov</title>
      <link>https://dev.to/kamrandb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamrandb"/>
    <language>en</language>
    <item>
      <title>How to clean your MacOS from too many Python versions and install it the right way</title>
      <dc:creator>Kamran Badirov</dc:creator>
      <pubDate>Thu, 01 Dec 2022 22:02:25 +0000</pubDate>
      <link>https://dev.to/kamrandb/how-to-clean-your-macos-from-too-many-python-versions-and-install-it-the-right-way-o1i</link>
      <guid>https://dev.to/kamrandb/how-to-clean-your-macos-from-too-many-python-versions-and-install-it-the-right-way-o1i</guid>
      <description>&lt;p&gt;Over the years of learning how to program I have installed too many Python libraries with different versions of Python. To make them ready to work immediately, I installed them blindly without control. At some point, it got so messy that I didn't even remember which one was for which project. In this article, I will provide a set of instructions on how to delete redundant libraries and versions of Python, and install them correctly.&lt;/p&gt;

&lt;p&gt;If you installed python from different sources (i.e. python.org, pip, easy_instal, macports, homebrew, fink) then binaries and libraries can be found in different locations of your system. It happens because these package managers put packages to different locations, for example, mac_ports puts things into /opt/local/, while homebrew uses /usr/local/.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;to see all the installed python versions and their location open the Terminal and run:&lt;br&gt;
&lt;code&gt;whereis python3&lt;/code&gt; and &lt;code&gt;whereis python&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;these commands will also show the location of the binaries. To see the versions of all installed python run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;which -a python3&lt;/code&gt; and &lt;code&gt;which -a python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To see python versions installed by Homebrew run: &lt;code&gt;brew list | grep python&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Now, when it comes to deleting all unnecessary python versions make sure &lt;strong&gt;NOT&lt;/strong&gt; to delete Python versions from the following directories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/usr/bin
/System/Library
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Apple-provided build of Python is installed in &lt;code&gt;/System/Library/Frameworks/Python.framework&lt;/code&gt; and &lt;code&gt;/usr/bin/python&lt;/code&gt;, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do. For more info refer &lt;a href="https://docs.python.org/3/using/mac.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Starting from MacOS 12.3 Apple removed Python2.7 from its OS, and in general, Python 2 is not supported anymore therefore it is worth considering switching to Python 3 if you haven't yet. &lt;/p&gt;

&lt;p&gt;To delete Python versions installed with Homebrew run the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew list python| xargs brew uninstall --force&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To delete other python versions from your macOS run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo rm -rf “/Applications/Python”
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /usr/local/bin/python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see broken symbolic links:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To remove them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /usr/local/bin

ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]' | awk '{print $9}' | tr -d @ | xargs rm*

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you haven't installed Homebrew I recommend doing so because it is useful (and very convenient) for both installing/removing packages (which we will do in the next step) and removing symlinks. After installing &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt; run the following command to see broken symbolic links to Python binaries:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew doctor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if any error is found just use brew prune and the error will be fixed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew prune&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Now that all redundant versions have been removed we can install Python the right way. There are many ways to install Python: anaconda, python.org, brew, etc. I recommend using brew because it always installs the latest version and is super easy to use. To install the latest version of python simply run:&lt;br&gt;
&lt;code&gt;brew install python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above command install the latest stable version of Python3 together with pip. Pip is a package manager for Python packages specifically. If you intend to use different versions and libraries it is worth considering using a virtual environment. Using virtual environment will save you the hassle of having different python versions and packages all over your system as everything will be contained in a single environment. To do so simply run: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip3 install virtualenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create a virtual environment:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;virtualenv -p python3 &amp;lt;desired-path&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Activate the virtual environment:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source &amp;lt;desired-path&amp;gt;/bin/activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Deactivate the virtualenv:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;deactivate&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Moving forward, be aware that different ways and tools install things independently to different locations, so use them mindfully. I recommend using brew as it makes it easy to install and update applications and utilities on a Mac.&lt;/p&gt;

</description>
      <category>python</category>
      <category>macos</category>
      <category>systems</category>
    </item>
  </channel>
</rss>
