Installing Python and Setting Up Your Development Environment
"I wish I could automate the simple tasks of website creation..." "It would be so convenient if I could automatically gather information from the web..."
As a web creator, you've probably had these thoughts at some point. The powerful tool that can make your wishes come true is the programming language Python.
In this article, we'll carefully walk you through the steps from installing Python to setting up your development environment, focusing on code you can copy and paste, so even beginners won't get discouraged. Don't worry if it sounds difficult. By taking it one step at a time, anyone can experience the thrill of "It works!" by running Python on their own PC.
Now, let's take the first step into the world of Python together!
Step 1: Let's Install Python on Your PC
First things first, you need to install Python to be able to use it on your PC. The procedure is slightly different depending on your OS (Windows or Mac), so please follow the steps for your environment.
For Windows Users
For Windows users, the easiest and most reliable way is to download the installer from the official Python website.
- Go to the official Python downloads page.
- Click the button that says "Download Python X.X.X" to download the installer. (The X.X.X part will be the latest version number).
- Double-click the downloaded installer (.exe file) to run it.
- γMost Important Point!γOn the first screen of the installer, be sure to check the box that says "Add Python X.X to PATH". If you forget this, you'll have a very hard time running commands later. Please, don't forget this!
- Click "Install Now" and wait for the installation to complete.
To confirm if the installation was successful, open the Command Prompt (press "Windows Key + R", type "cmd", and press Enter) and try typing the following command.
python --version
If the version number of the Python you installed is displayed like this, the installation is complete!
Python 3.12.4
For Mac Users
Recent Macs often come with Python pre-installed. However, that version might be a bit old (Python 2 series). Since the Python 3 series is now the mainstream, it's recommended to install the new version.
If you're setting up a development environment on a Mac, using a package manager called Homebrew is by far the most modern and efficient way. If you haven't installed Homebrew yet, let's get that done first.
With Homebrew ready, open the Terminal and run the following command.
brew install python
Homebrew will kindly install the latest Python 3 for you. Once it's finished, let's check the version. On a Mac, the command is often `python3`.
python3 --version
If the version number is displayed just like for Windows, the installation was a success!
Python 3.12.4
Step 2: Create a Virtual Environment
"A virtual environment? What's that? Sounds like a hassle..." you might think. But this is a very important step to make your future Python life comfortable.
In short, a virtual environment is like a "dedicated toolbox for each project." For example, let's say you're working on a project to build Website A and a project for data analysis B at the same time. It often happens that A requires version 1.0 of a tool (library), while B requires version 2.0. If you keep these in the same place, the versions will conflict and cause errors.
That's where you prepare a "toolbox for A" and a "toolbox for B" for each project, and put only the necessary tools in each. This is the concept of a virtual environment. This way, you can develop in a clean environment without worrying about other projects.
Here, let's try creating a virtual environment using the `venv` feature that comes standard with Python.
First, create a project folder somewhere you like, such as your desktop. Let's name it `my-python-project`. Then, navigate to that folder in your terminal (or Command Prompt).
1. Create the Virtual Environment
Inside the folder you created, run the following command. The last part, `myenv`, is the name of the virtual environment and can be changed to whatever you like. (It's common to use `venv` or `.venv`.)
For Windows
python -m venv myenv
For Mac
python3 -m venv myenv
2. Activate the Virtual Environment
A virtual environment isn't usable just by creating it. You need to declare, "I'm going to use this toolbox now!" This is called activation.
For Windows (Command Prompt)
myenv\Scripts\activate
For Mac (zsh / bash)
source myenv/bin/activate
When you successfully activate it, the name of the virtual environment you created, like `(myenv)`, will appear at the beginning of your command line. This is a sign that you are inside the dedicated toolbox. In this state, even if you install libraries, they will be saved only within this virtual environment, without polluting your PC's main environment.
When you're finished with your work, you can run the `deactivate` command to return to the original environment.
Step 3: Get Your Best Partner, VS Code
To write Python code, you need a text editor. There are many editors out there, but the most popular one right now, used by everyone from beginners to professionals, is Visual Studio Code (VS Code).
It's free, extremely powerful, and you can customize it to your liking by adding extensions. If you haven't installed it yet, now is the perfect time to do so.
- Go to the official VS Code website, download the installer for your OS, and install it.
- Launch VS Code and click on the icon that looks like stacked squares on the left side (the Extensions view).
- In the search box, type "Python" and install the official Python extension provided by Microsoft. This will enable features like code auto-completion and error checking, dramatically improving your development efficiency.
Once VS Code is ready, open the project folder you created earlier (`my-python-project`) in VS Code. (File menu > "Open Folder...")
Step 4: Your First Python Program!
Now, it's time for the long-awaited coding! With the environment set up, let's run the classic "Hello, World!" in Python.
In the VS Code Explorer (the file list on the left), create a new file and name it `hello.py`. Then, copy and paste the following line into that file.
print("Hello, Python World!")
`print()` is a command to display the contents inside the parentheses on the screen. Very simple, isn't it?
Next, let's run this program. Select "Terminal" > "New Terminal" from the VS Code menu to display a terminal within VS Code. (At this point, confirm that the terminal is in the virtual environment `(myenv)`).
In the terminal that appears, run the following command.
python hello.py
When you run it, you should see the following displayed in the terminal.
Hello, Python World!
Congratulations! π This is the moment you've taken your first step as a Python programmer. This experience of "my code worked as intended" is the best motivation for learning to program.
[Advanced Example] Let's Get a Website's Title with Python
Just "Hello, World!" doesn't show the full power of Python. As a more practical example, let's experience the basics of "Web Scraping," which automatically retrieves information from websites.
Here, we'll use external libraries (useful tools). `requests` is for getting information from websites, and `BeautifulSoup4` is a library for extracting the data you want from the fetched HTML information.
First, let's install these libraries into our virtual environment. Run the following command in your terminal. `pip` is the tool for managing Python libraries.
pip install requests beautifulsoup4
Once the installation is complete, create a new file named `get_title.py` and paste the following code. This code accesses the Yahoo! JAPAN homepage, gets the page title (the content of the `<title>` tag), and displays it.
import requests
from bs4 import BeautifulSoup
# URL of the website you want to get
url = "https://www.yahoo.co.jp/"
# Access the URL and get the HTML
response = requests.get(url)
response.encoding = response.apparent_encoding # To prevent garbled text
# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the title tag and display its text
print(soup.title.string)
Let's run it in the terminal, just like we did with `hello.py`.
python get_title.py
If it works correctly, the site title of Yahoo! JAPAN at that moment should be displayed. (The displayed content will vary depending on when you run it.)
Yahoo! JAPAN
With just a few lines of code, you were able to automatically retrieve information from a website. By applying this, you can create tools that directly improve the efficiency of a web creator's work, such as periodically gathering information from multiple sites or extracting only news containing specific keywords.
Points to Note and Tips
Finally, here are a few points that beginners often stumble on and some useful things to know.
- The Importance of the PATH Setting: The "Add Python to PATH" option mentioned during the Windows installation is really important. If you forgot to do it, the quickest fix is to uninstall and then reinstall, making sure to check the box this time.
- The Difference Between Python 2 and 3: When searching for information online, you might find old code for Python 2. If you see `print "Hello"` written without parentheses, that's Python 2 code. Using Python 3 is the standard now, so don't get confused by old information.
- Always Use a Virtual Environment: Even as you get more comfortable with development, please continue the habit of creating virtual environments. If you start installing libraries in the global environment (your PC's main environment) thinking "it's just a quick test," your environment will get messy in no time, and you'll definitely regret it later. When trying something new, use a new virtual environment.
- Managing Libraries: Using the command `pip freeze > requirements.txt`, you can output a list of the libraries installed in that virtual environment to a file. By sharing this file with others, they can reproduce the exact same environment with a single command: `pip install -r requirements.txt`. This is an essential technique for team development.
Conclusion
Great job! If you've followed the steps in this article, you should now have a perfect development environment on your PC, ready to run Python at any time.
You've installed Python, created a virtual environment, ran "Hello, World!" in VS Code, and even experienced the basics of web scraping. Cherish this feeling of "it worked!" and please try moving on to the next step.
Besides the web scraping we introduced, Python can solve various "hassles" in a web creator's daily work, such as automating Excel files, automatically resizing images, and tedious file renaming tasks. Furthermore, by learning frameworks like Flask or Django, building full-fledged web applications yourself is not just a dream.
I hope this article helps broaden the scope of your creative activities. Happy Coding!