Building RESTful Python Web Services by Gastón C. Hillar

Building RESTful Python Web Services by Gastón C. Hillar

Author:Gastón C. Hillar
Language: eng
Format: mobi, epub
Publisher: Packt Publishing
Published: 2017-12-14T08:48:12+00:00


Setting up a virtual environment with Flask and Flask-RESTful

In Chapter 1, Developing RESTful APIs with Django, we learned that, throughout this book, we were going to work with the lightweight virtual environments introduced in Python 3.4 and improved in Python 3.4. Now, we will follow the steps to create a new lightweight virtual environment to work with Flask and Flask-RESTful. It is highly recommended to read Chapter 1, Developing RESTful APIs with Django, in case you don't have experience with lightweight virtual environments in Python. The chapter includes all the detailed explanations of the effects of the steps we are going to follow.

First, we have to select the target folder or directory for our virtual environment. We will use the following path in the example for macOS and Linux. The target folder for the virtual environment will be the PythonREST/Flask01 folder within our home directory. For example, if our home directory in macOS or Linux is /Users/gaston, the virtual environment will be created within /Users/gaston/PythonREST/Flask01. You can replace the specified path with your desired path in each command, as shown:

~/PythonREST/Flask01

We will use the following path in the example for Windows. The target folder for the virtual environment will be the PythonREST\Flask01 folder within our user profile folder. For example, if our user profile folder is C:\Users\Gaston, the virtual environment will be created within C:\Users\gaston\PythonREST\Flask01. You can replace the specified path with your desired path in each command, as shown:

%USERPROFILE%\PythonREST\Flask01

Open a Terminal in macOS or Linux and execute the following command to create a virtual environment:

python3 -m venv ~/PythonREST/Flask01

In Windows, execute the following command to create a virtual environment:

python -m venv %USERPROFILE%\PythonREST\Flask01

The preceding command doesn't produce any output. Now that we have created a virtual environment, we will run a platform-specific script to activate it. After we activate the virtual environment, we will install packages that will only be available in this virtual environment.

If your Terminal is configured to use the bash shell in macOS or Linux, run the following command to activate the virtual environment. The command also works for the zsh shell:

source ~/PythonREST/Flask01/bin/activate

If your Terminal is configured to use either the csh or tcsh shell, run the following command to activate the virtual environment:

source ~/PythonREST/Flask01/bin/activate.csh

If your Terminal is configured to use either the fish shell, run the following command to activate the virtual environment:

source ~/PythonREST/Flask01/bin/activate.fish

In Windows, you can run either a batch file in the Command Prompt or a Windows PowerShell script to activate the virtual environment. If you prefer the Command Prompt, run the following command in the Windows command line to activate the virtual environment:

%USERPROFILE%\PythonREST\Flask01\Scripts\activate.bat

If you prefer the Windows PowerShell, launch it and run the following commands to activate the virtual environment. However, note that you should have the scripts execution enabled in Windows PowerShell to be able to run the script:

cd $env:USERPROFILE PythonREST\Flask01\Scripts\Activate.ps1

After you activate the virtual environment, the Command Prompt will display the virtual environment root folder name, enclosed in parenthesis, as a prefix for the default prompt, to remind us that we are working in the virtual environment.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.