- Python Programming Blueprints
- Daniel Furtado Marcus Pennington
- 602字
- 2021-06-24 18:53:48
Setting up the environment
Let's go ahead and configure our development environment. The first thing we need to do is create a new virtual environment, so we can work and install the packages that we need without interfering with the global Python installation.
Our application will be called musicterminal, so we can create a virtual environment with the same name.
To create a new virtual environment, run the following command:
$ python3 -m venv musicterminal
And to activate the virtual environment, you can run the following command:
$ . musicterminal/bin/activate
Perfect! Now that we have our virtual environment set up, we can create the project's directory structure. It should have the following structure:
musicterminal
├── client
├── pytify
│ ├── auth
│ └── core
└── templates
Like the application in the first chapter, we create a project directory (called musicterminal here) and a sub-directory also named pytify, which will contain the framework wrapping Spotify's REST API.
Inside the framework directory, we split auth into two modules which will contain implementations for two authentication flows supported by Spotify—authorization code and client credentials. Finally, the core module will contain all the methods to fetch data from the REST API.
The client directory will contain all the scripts related to the client application that we are going to build.
Finally, the templates directory will contain some HTML files that will be used when we build a small Flask application to perform Spotify authentication.
Now, let's create a requirements.txt file inside the musicterminal directory with the following content:
requests==2.18.4
PyYAML==3.12
To install the dependencies, just run the following command:
$ pip install -r requirements.txt

As you can see in the output, other packages have been installed in our virtual environment. The reason for this is that the packages that our project requires also require other packages, so they will also be installed.
Requests were created by Kenneth Reitz https://www.kennethreitz.org/, and it is one of the most used and beloved packages in the Python ecosystem. It is used by large companies such as Microsoft, Google, Mozilla, Spotify, Twitter, and Sony, just to name a few, and it is Pythonic and really straight-forward to use.
Another module that we are going to use is curses. The curses module is simply a wrapper over the curses C functions and it is relatively simpler to use than programming in C. If you worked with the curses C library before, the curses module in Python should be familiar and easy to learn.
One thing to note is that Python includes the curses module on Linux and Mac; however, it is not included by default on Windows. If you are running Windows, the curses documentation at https://docs.python.org/3/howto/curses.html recommends the UniCurses package developed by Fredrik Lundh.
Just one more thing before we start coding. You can run into problems when trying to import curses; the most common cause is that the libncurses are not installed in your system. Make sure that you have libncurses and libncurses-dev installed on your system before installing Python.
If you are using Linux, you will most likely find libncurses on the package repository of our preferred distribution. In Debian/Ubuntu, you can install it with the following command:
$ sudo apt-get install libncurses5 libncurses5-dev
Great! Now, we are all set to start implementing our application.
- EJB 3.1從入門(mén)到精通
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- HCNA網(wǎng)絡(luò)技術(shù)
- Practical Web Design
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- 2018網(wǎng)信發(fā)展報(bào)告
- 電力物聯(lián)網(wǎng)工程技術(shù)原理與應(yīng)用
- Kong網(wǎng)關(guān):入門(mén)、實(shí)戰(zhàn)與進(jìn)階
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)指南
- Microsoft Power Platform Enterprise Architecture
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 組網(wǎng)技術(shù)與網(wǎng)絡(luò)管理
- 移動(dòng)互聯(lián)網(wǎng)新思維
- 商業(yè)的本質(zhì)和互聯(lián)網(wǎng)