Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Python Programming for IoT

You're reading from   Practical Python Programming for IoT Build advanced IoT projects using a Raspberry Pi 4, MQTT, RESTful APIs, WebSockets, and Python 3

Arrow left icon
Product type Paperback
Published in Nov 2020
Publisher Packt
ISBN-13 9781838982461
Length 516 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gary Smart Gary Smart
Author Profile Icon Gary Smart
Gary Smart
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Section 1: Programming with Python and the Raspberry Pi
2. Setting Up your Development Environment FREE CHAPTER 3. Getting Started with Python and IoT 4. Networking with RESTful APIs and Web Sockets Using Flask 5. Networking with MQTT, Python, and the Mosquitto MQTT Broker 6. Section 2: Practical Electronics for Interacting with the Physical World
7. Connecting Your Raspberry Pi to the Physical World 8. Electronics 101 for the Software Engineer 9. Section 3: IoT Playground - Practical Examples to Interact with the Physical World
10. Turning Things On and Off 11. Lights, Indicators, and Displaying Information 12. Measuring Temperature, Humidity, and Light Levels 13. Movement with Servos, Motors, and Steppers 14. Measuring Distance and Detecting Movement 15. Advanced IoT Programming Concepts - Threads, AsyncIO, and Event Loops 16. IoT Visualization and Automation Platforms 17. Tying It All Together - An IoT Christmas Tree 18. Assessments 19. Other Books You May Enjoy

Using sudo within virtual environments

I'm sure that while working on your Raspberry Pi you have had to execute commands in a Terminal with the sudo prefix because they required root privileges. If you ever need to run a Python script that is in a virtual environment as root, you must use the full path to your virtual environment's Python interpreter.

Simply prefixing sudo before python, as shown in the following example, does not work under most circumstances, even if we are in the virtual environment. The sudo action will use the default Python that's available to the root user, as shown in the second half of the example:

# Won't work as you might expect!
(venv) $ sudo python my_script.py

# Here is what the root user uses as 'python' (which is actually Python version 2).
(venv) $ sudo which python
/usr/bin/python

The correct way to run a script as root is to pass the absolute path to your virtual environment's Python interpreter. We can find the absolute path using the which python command from inside an activated virtual environment:

(venv) $ which python
/home/pi/pyiot/chapter01/venv/bin/python

Now, we sudo our virtual environment's Python interpreter and the script will run as the root user and within the content of our virtual environment:

(venv) $ sudo /home/pi/pyiot/chapter01/venv/bin/python my_script.py

Next, we'll see how to run a Python script that's sandboxed in a virtual environment from outside of its virtual environment. 

You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020
Publisher: Packt
ISBN-13: 9781838982461
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image