Vos données à votre portée avec Salesforce, Python, SQL, & et plus

Need event music? 🎸

Live and recorded jazz, pop, and meditative music for your virtual conference / Zoom wedding / yoga class / private party with quality sound and a smooth technical experience

Installer Python sous Windows avec Anaconda

05 Mar 2019 🔖 python tutoriels salesforce windows débutant
💬 FR ( Read this post in English )

Post Header Image

Table de matières

Je conseille toujours mes étudiants de ne jamais saisir de vraies données d’entreprise aux “IDE” en ligne comme Repl.it et CodeBunk, mais d’installer Python sur leurs propres ordinateurs.

Enfin j’ai écrit un tutoriel pour l’installation de Python sous Windows avec une approche très simple !

C’est SI SIMPLE – il ne faut que :

  • télécharger un installateur
  • exécuter l’installateur
  • ouvrir l’éditeur de code qui vient d’être installé
  • exécuter votre premier print('Hello World')

Installez Anaconda & exécutez un programme Python

Télécharger

  • Malheureusement, l’installateur Anaconda est tout en anglais.
  • Heureusement, on peut changer la langue de l’éditeur de code (Spyder) qu’il installe, et c’est avec Spyder que vous travaillerez le plus souvent.

D’abord, télécharger l’installateur “Anaconda” approprié.

Le site web d’Anaconda ne détecte pas très bien votre système d’exploitation, donc cliquez sur l’option “Windows” option juste au-dessus des téléchargements.

Je recommande la version “64-bit.”

Il vous faut la version “Python 3.BlaBlaBla” et non “Python 2.BlaBlaBla”.

Exécutez l’installateur

Double-cliquez le fichier “.exe” pour exécuter l’installation. Cliquez sur “Next(“suivant”) et puis sur “I Agree(“d’accord”) sur l’écran suivant.

Choisissez “que pour moi” ou “pour tout utilisateur”

Moi, je vais installer Anaconda que pour moi (“just me”), parce que je n’ai pas de droits administratives pour installer des logiciels sur mon ordinateur d’entreprise.

Le choix que je fais sur cet écran influence le dossier où l’installateur suggère d’installer Anaconda:

  • C:\Users\MON_USERNAME\AppData\Local\Continuum\anaconda3 (just me), or
  • C:\Program Files\Anaconda3 (all users)

Dossier d’installation

L’installation des programmes dans les dossiers cachés comme \AppData\Local\ m’embête. Donc je vais changer le chemin d’installation sur l’écran suivant à C:\Users\MON_USERNAME\Documents\ProgFiles\Continuum\anaconda3.

(Oui, dans la capture d’écran, le chemin dit “ProgramFilesForSoftwareHatingSpaces” – “programfiles pour les programmes qui ne supportent pas les espaces” – à la place de “ProgFiles”. J’ai réutilisé la capture d’un tutoriel précédent où l’installateur m’a embêtée en se plaignant de mon choix originel de “Program Files”.)

Options

Ensuite, l’installateur me propose deux options:

  1. Ajouter “Anaconda” à mon “variable d’environnement” dit “PATH” (“CHEMIN”). Laissez-le décochée, comme conseille le texte en anglais.
  2. Enregistrer “Anaconda” comme environnement de programmation “Python” par défaut. Laissez-le cochée, comme conseille le texte en anglais.

Cliquez sur “Install” (“installer”).

Pendant l’installation

L’installateur doit quelques minutes. Il installe des milliers de petits fichier sur votre disque dur !

Q: Pourquoi autant de fichiers ?

R: Votre ordinateur ne comprend pas, par défaut, des commandes du langage Python.

Votre ordinateur comprend le “langage assembleur” – un langage que vous ne voudriez pas devoir écrire à la main.

Tout ordinateur Windows comprend aussi le language “C” (que vous ne voudriez pas écrire à la main si vous ne désirez pas devenir programmeur professionnel).

Beaucoup de ces petits fichiers sont des programmes en “C” qui enseignent votre ordinateur à comprendre des commandes “Python” (si vous, ou si un programme comme un “IDE” à votre place, informez Windows du lieu exact où vous avez installé Python – pour moi, c’est le chemin C:\Users\MON_USERNAME\Documents\ProgramFilesForSoftwareHatingSpaces\Continuum\anaconda3\python.exe).

De plus, Anaconda inclue beaucoup de “modules” Python (augmentations de la base de commandes Python avec lesquelles vous pouvez programmer) – ça ajoute aussi beaucoup de fichiers à installer.

Pour finir

Quand vous voyez le texte “Completed” (“fini”), cliquez sur “Next(“suivant”).

Ne prenez pas la peine d’installer VSCode. C’est un “IDE” (éditeur de code), mais Anaconda en a installé un qui s’appelle “Spyder” qui vous conviendra bien. Cliquez sur “Skip” (“sauter”).

Déchochez les deux cases “learn more” (“apprendre plus”), et cliquez sur “Finish(“finir”)

(La deuxième case ouvre cette page dans votre navigateur; la première n’ouvre que le site web d’Anaconda.)


Écrivez votre premier programme Python

Si vous avez essayé de coder dans un “IDE” en ligne comme Repl.it ou CodeBunk, vous savez bien que coder peut être aussi simple que de composer quelques lignes et de cliquer sur un gros bouton “run(exécuter) !

Et vous avez maintenant, sur votre ordinateur, un tel programme. Il s’appelle Spyder.

L’installateur Anaconda l’a installé.

Regardez votre “menu démarrer” Windows (identifiable par le logo de Windows; situé en bas à gauche de l’écran, dans la barre des tâches).

Il devrait y être un nouveau dossier qui s’appelle “Anaconda3 (64-bit)” qui contient plusieurs icones.

Cliquez sur l’icone appelé “Spyder.”

L’écran de lancement pendant le démarrage de Spyder est ainsi:

Et dans la barre des tâches, l’icone de Spyder est ainsi:

Une fois exécutant, Spyder se présente généralement comme ceci::

  • À gauche, il y a une zone pour éditer du code. Le système vous proposera d’enregistrer le fichier qui contient ce code sous le chemin C:\users\VOTRE_USERNAME\untitled0.py (“untitled” = “sans titre”) mais ne l’a pas encore enregistré. En ce moment, les contenus du code sont:
# -*- coding: utf-8 -*-
"""
Created on SOME_TIMESTAMP

@author: VOTRE_USERNAME
"""
  • À droite, ou en bas à droite, il y aura un “console” pour regarder la sortie du code que vous exécutez à chaque fois que vous cliquez sur le gros bouton vert “exécuter” en haut de l’écran (c’est un triangle vertant pointant vers la droite).

(TO DO:) Barre de menu en haut … Tools -> Preferences -> General -> onglet Advanced Settings -> Language : Français. Bouton OK. Acceptez de redémarrer Spyder.

Pick a nice folder for your program (personally, I’m going to save mine under C:\example\, but you might want to put it on your desktop) and save it there with a filename of “hello.py”.

(Spyder’s “run” button complains if you try to run code before giving it a proper spot on your hard drive. You could just do file->save, but I personally can’t stand cluttering my c:\users\MON_USERNAME\ with files like “untitled0.”)

(Note that in the photo, mine is called “hello2.py” because I reused a screenshot from a different tutorial with 2 steps!)

Now erase the entire contents of the file, and on line 1, type:

print('Hello world')

Then click the big green “run” button.

You may see a pop-up dialog with all sorts of options about executing code consoles or interpreters and such.

Just click “cancel” on the pop-up dialog box and then click the play button again.

The pop-up box was giving you a chance to highly customize where you want to see any output from running your code.

Clicking “cancel” and trying again seems to tell Spyder, “you figure out where to display the output of this code, don’t bother me with all these options.”

(Unfortunately, I didn’t catch this dialog box with a screenshot – Spyder seemed to remember my settings from my previous usage of it with “big Anaconda.”)

Your output should look something like this – somewhere at the right-hand side of the screen, probably in a console called “Console 1/A,” you’ll see confirmation of the filepath of “hello.py,” followed by a new line with the text “Hello World”.

(Note that in the photo, mine says “Hello again” because I reused a screenshot from a different tutorial with 2 steps!)

Congratulations – you’ve written and run a Python program!

In the top menu, click “File” -> “Save” because why not be proud of this working code? It’s good to get used to saving your work as soon as you like it.

But before we write a bigger program, let’s take a little diversion to learn about “modules.” They’re pretty important in Python.


Modules

Checking whether “modules” are installed

Now you need to learn what it looks like when a given extension to the Python language, also known as a “library” or “package” or, particularly in Python, a “module,” is installed.

Backspace out the entirety of your code and on line 1, type:

import pandas
  • Trivia: the orange warning triangle to the left of the line numbers in your code is telling you that you “imported” a package that your program doesn’t actually use anywhere else in the program. That’s Spyder trying to help you write better, more efficient code.
  • Red circles with “x” in them to the left of the line numbers means a line doesn’t even contain legitimate Python to begin with. Those, you’ll want to care about, or your program won’t run.

Yay, it exists!

Click the “run” button.

If you do have the “pandas” module installed, nothing special happens.

You should have it installed – it comes with the “big”/mainstream version of Anaconda. Modules like Pandas is why Anaconda took so long to install!

If you have “pandas” installed, when you run this code, you just get another command prompt (“In[SomeNumber]”) in the box at the right where the number in square brackets is 1 bigger than it was before.

(Note: in the screenshot above, things look a slightly different because I’m still sooooo lazy and reusing screenshots from a different tutorial – sorry!)

Boo, it doesn’t exist!

Let’s try “importing” a module you probably don’t have, because it doesn’t come with Anaconda.

Backspace out the entirety of your code and on line 1, type:

import simple_salesforce

Click the “run” button.

If you get a huge dump of colorful text in your console, with the last few lines of the big error message saying the following, you now know that you don’t have the module called “simple_salesforce” installed:

    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/example/hello.py", line 1, in <module>
    import simple_salesforce

ModuleNotFoundError: No module named 'simple_salesforce'

Installing a “module”

Anaconda comes with just about everything you need as a total beginner, so don’t bother installing extra “modules” on your computer just yet.

However, if you ever need any, see the “modules” section of my instructions on setting up “miniconda” on Windows – people who install “miniconda” have to install all their own “modules” by hand, so I went over it step-by-step in those instructions!


Write your second Python program

Backspace out the entirety of your code.

Copy and paste the following code into your code editor and run it:

import pandas
pandas.set_option('expand_frame_repr', False)
filepath = 'https://raw.githubusercontent.com/pypancsv/pypancsv/master/docs/_data/sample1.csv'
df = pandas.read_csv(filepath)
print(df)

You should see the following output in your console at right:

      Id    First      Last           Email                      Company
0   5829    Jimmy    Buffet  [email protected]                          RCA
1   2894  Shirley  Chisholm  [email protected]       United States Congress
2    294  Marilyn    Monroe  [email protected]                          Fox
3  30829    Cesar    Chavez  [email protected]          United Farm Workers
4    827  Vandana     Shiva  [email protected]                     Navdanya
5   9284   Andrea     Smith  [email protected]     University of California
6    724   Albert    Howard  [email protected]  Imperial College of Science

Now you’ve opened a CSV spreadsheet file with Python – you’re off to the races!

Head here to learn new skills and put them in action on your computer.


Happy Programming

You now have a working environment for editing CSV files in Python!

  1. You have an “IDE,” or as I like to think of it, a “text editor with a run button,” called Spyder, that runs code you type into it as you see fit.
  2. You know where to look to learn to install & update “modules” that you hear might be useful.
    (Be careful and don’t trust any old module you find on the internet. It’s nothing more than “someone else’s code that you’re choosing to run on your computer.” Make sure it’s not from someone sketchy.)
--- ---