jDataLab Jager

7 minute read

This post is the one for you if you are ready to start your programming journey with Python, and want to set up a Python environment in your Mac or PC. Python has become so popular that it has made its contributions to all kinds of applications.

The Options

There are a plenty of ways in which we gain access to Python, either locally or remotely, either in your own computer or via a cloud service provider, and even in Web-based IDEs from online learning web sites, etc.

For instance,

  1. To obtain first sight of Python, Web-based IDEs provides a carefree option where you can do simple scripting without installing anything locally, such as

    • repl.it where registered users work within an interactive window with tree view of resources, editing and output areas.
    • datacamp is the one for doing data science with R and Python.
    • Besides, W3Schools includes great reference to syntax, data types and basic programming structures for most languages.
  2. Download an official installer from python.org and install multiple versions in your computer. A Python installation with a code editor together is one option which you should give it a try. Choosing a code edit or an IDE depends on personal preference. Others’ fit may not be the right one for you.

    • Visual Studio Code is an all-in-one editor which was initially released in 2015; with extensions, we can do most of programming languages in the Code.
    • Spyder IDE is a scientific Python development environment with MATLAB-like GUI, which may be preferred by people from the fields of engineering and scientific computations.
> The first two options above are straightforward and simle; but when you want to switch between multiple versions and build a project upon a specific version and package denpendencies, you should create multiple virtual environments, and associate each project with a single environment. With per-project virtual envronments, the projects are isolated from each other with respect to their dependencies, including the Python version as well as the packages. 

> The virtual environments are separated and updating an individual environment will not interference with others. The following options support virtual environments. 
  1. Install an environment manager such as Anaconda, the most popular Python data science platform, which comes with a bundled Python. This would be the best option if you do Python for data science, machine learning and AI. Within Anaconda, the dependency management tool Conda creates virtual environments as well as activating, deactivating and deleting the environments.

  2. Complete option #2 and install a virtual environment management tool, such as virtualenv which creates isolated Python environments and pyenv package for isolating Python versions.

  3. Access Python on cloud computing platforms: AWS, Microsoft Azure and GCP.

These options may easily confuse someone who just begins. No matter what options you will try or have tried previously, you would eventually find yourself ending up with managing projects with virtual environments and associating each project with one Python virtual environment. In the long run, working with Python virtual environments is the right thing to do.

If you are a beginner, before moving towards to more advanced options, such as #3, #4 and #5 listed above, I would recommend you to try both options #1 and #2.

Do not underestimate option #1. Indeed, web-based IDEs give us an ad hoc solution whenever you have no access to programming environment. Not only for Python, but also for most of the languages, repl.it allows us to easily learn and test code. Sometimes option #1 could even be sufficient for someone who is taking basic programming class.

For someone who is serious about learning programming with Python, going through Option #2, install Python and remove Python in a computer, helps understanding the structure of files and libraries in a Python installation. This is what this post is purposed to.

The following shows how to remove an old Python and install a fresh one in a computer. The first part is for MacoS and the second is for Windows. For Linux, it is similar to MacOS, but some commands are different from MacOS.

=== MacOS ===

1. Pre-installed Python

MacOS, Linux and other Unix-like systems come with a pre-installed Python. Do not remove the system Python and the operating system depends on it. MacOS has the system Python in:

/System/Library/Frameworks/Python.framework

Completely Uninstall Python from Mac

If you previously have installed Python using the official installer from python.org, each Python installation will create the following four types of content in Mac:

  • An app folder in the Applications directory
  • A framework in the /Library/Frameworks/Python.framework which contains executable and libraries.
  • A few symbolic links to the executable(s) in the /usr/local/bin directory
  • The script in the .bash_profile which adds the Python executable to path

All of them should be removed in a deep clean. You need to run a few commands in a Terminal window.

To open a new Terminal window in Mac, you can: - Press command+space to open Spotlight Search - Search Terminal. - Open Terminal.app.

Steps

1. Remove the folder in the Applications.

Run the command

sudo rm -rf '/Applications/Python X.Y' 

and replace X.Y with the version number in the folder’s name. (Alternatively, you can do this in Finder: Navigate to the Applications folder. Drag the python X.Y folder to Trash. Go to Trash directory. Delete files in Trash.)

2. Remove Python Framework

# remove all the versions
sudo rm -rf /Library/Frameworks/Python.framework
# remove a specific version
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.9    

We can install Homebrew, which is the Missing Package Manager for macOS (or Linux).

Install Homebrew: * Open the Homebrew. * Check the box next to the command. * Paste the command in the Terminal and run it.

Run the command to removes all broken symlinks:

brew cleanup

Alternatively, the following command also will remove from the /usr/local/bin/ directory, the folders having python in their name:

sudo rm -rf /usr/local/bin/python
sudo rm -rf /usr/local/bin/python3

4. Remove the Python path in bash profile

To open the script, .bash_profile, run the command:

open ~/.bash_profile

You will easily see the chunk similar to the following. Remove the chunk.

# Setting PATH for Python 3.9
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH

Install Python in Mac

By now, you have fully uninstalled a Python installation from your Mac. You are ready to install a new one. To do so, download the installer from python.org, run the installer and make selections in each dialogue.

Check the new Install

You normally have installed the version above 3 (Python3). In the Terminal, run the command:

python3 --version

It should return the version, the same as the one you have installed.

=== Windows ===

Windows was known as the only operating system that does not include a Python interpreter out of the box. With the May 2019 Windows Update, every install of Windows will include python commands that take you directly to the Python store page.

Open a Command Prompt window (Press Windows+R. Type cmd. Click OK)

If you have the latest Windows 10 updates, run the command in the Command Prompt:

where python

You will see the following path in the result:

C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe

This is the one which links to the Python store page from Microsoft.

Uninstall Python in Windows

    1. (Windows+R.appwiz.cpl) Open the Uninstall Program. Locate Python and uninstall it.
    1. Remove the associated path entry from the Windows path: (sysdm.cpl ,3) Open Environment Variables. If you have installed it for all users, click Edit button in the System variables frame. From there you can delete all the entries related to the Python to be removed.
    1. In the Windows Explorer, double check whether the Python directory has been removed. If not, manually delete the entire folder.

Install Python in Windows

To install a new Python in Windows, you can follow the official instructions. The following is a quick reminder.

  1. Download the installer from python.org
  2. Run the installer.
  3. During the installation, optionally you can
    • Do customize the location.
      • The default location is C:\Users\user\AppData\Local\Programs\Python\PythonXY.
      • You can change the location in the dialogue, for example,C:\Python39.
      • It is recommended to check the box of Add Python to PATH.

Check the new install

In the Command Prompt window, run the command:

where python

You will see the new Python is included in the returned list:

C:\Python39\python.exe
C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe

If there are multiple python versions in the path, each time when you run python command in the Command Prompt, the system will run the first accessible Python.

python --version

If you need more details, refer to the video.

comments powered by Disqus