Course 2.3Lesson 4 of 4
Most problems of a beginner are about the PATH, the folder, the file name, or the version, and each has a short check. Errors in a file are read in the same way as in a code box: from the last line, with the line number. The tool pip installs packages that other people wrote, and a package is used with import. After this chapter, a learner can keep programs as files, run them, and add packages, and can go on with the rest of the book on a computer or in the browser.
Try the code and the simulations beside the lesson.
After this lesson you can
pip, andAlmost every problem in the first week is one of these six. Each one has a very short check.
| What you see | The cause | What to try |
|---|---|---|
| "python is not recognised" (Windows) | Python is not on the PATH | Try py. Or install again, with the PATH box ticked. Open a new terminal. |
| "command not found: python" (macOS, Linux) | the command is python3 | Type python3. |
| "No such file or directory" | you are in the wrong folder | cd to the folder, dir or ls to see the files. |
| the file does nothing, or it opens in a text editor | the name ends in .txt | Show file name extensions, and rename it to .py. |
SyntaxError on a line that looks right | a Python 2 habit or a curly quotation mark | Use print(...) with brackets, and straight quotation marks. |
strange errors with import | your file has the name of a module | Rename random.py or math.py to something else. |
An error in a file looks like the one in the code box on this site, with one difference: the file name is shown.
Python comes with many tools, such as math and random. For other jobs, people have written packages: ready-made programs that you can add. The tool that installs them is pip. You type its commands in a terminal, and not in a code box. The usual form is python -m pip install name (use py -m pip on Windows if you prefer, and python3 -m pip on macOS and Linux).
After you install a package, you use it in your file with import numpy. A package is installed once, and used in any program afterwards.
Make a plan that you can keep. The next chapters each take a few hours.
You now have Python on your computer, and you can run a file. Here is a sensible path.
if.python with a subfolder for each chapter. A good name makes a program easy to find next month.>>>). They go in the terminal.python -m pip uses the Python that you mean.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
python3, the wrong folder, .txt names, Python 2 habits, and names of modules.python -m pip install name adds a package. It is typed in a terminal, and used with import.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
python is not recognised. What do you try first?random.py?pip command?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.