Skip to content

Commit

Permalink
add tutorial on create virtual envirnment
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemeiping committed Apr 3, 2024
1 parent c6e1e4e commit 346aa2e
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,9 @@ In the 3-in-1 starter kit, you will find a complete Arduino course to help begin

<a id="update"></a>
## Update:
2024-04-03:
- Add the tutorial on installing a virtual environment

2024-03-18:
- Add the video courses

Expand Down
41 changes: 37 additions & 4 deletions docs/source/2.2.7_mfrc522_rfid_module.rst
Expand Up @@ -161,7 +161,27 @@ to the card.

For Python Language Users
^^^^^^^^^^^^^^^^^^^^^^^^^
**Step 2:** Install the libraries.

**Step 2**: Activating the Virtual Environment.

.. note::

* Before activation, you need to ensure that you have created a virtual environment, please refer to: :ref:`create_virtual`.

* Each time you restart the Raspberry Pi, or open a new terminal, you will need to run the following command again to activate the virtual environment.

.. raw:: html

<run></run>

.. code-block:: shell
source myenv/bin/activate
Once the virtual environment is activated, you will see the environment name before the command line prompt, indicating you are working within the virtual environment.


**Step 3:** Install the libraries.

The ``spidev`` library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.

Expand Down Expand Up @@ -191,8 +211,21 @@ Among them ``MFRC522.py`` is the realization of RFID RC522 interface, this libra
sudo pip3 install mfrc522
**Step 4**: Exiting the Virtual Environment.

**Step 3:** Go to the folder of the code.
When you have completed your work and wish to exit the virtual environment, simply run:

.. raw:: html

<run></run>

.. code-block:: shell
deactivate
This will return you to the system's global Python environment.

**Step 5:** Go to the folder of the code.

.. raw:: html

Expand All @@ -202,7 +235,7 @@ Among them ``MFRC522.py`` is the realization of RFID RC522 interface, this libra
cd ~/davinci-kit-for-raspberry-pi/python/2.2.7
**Step 4:** Run the executable file.
**Step 6:** Run the executable file.

.. raw:: html

Expand Down Expand Up @@ -313,6 +346,6 @@ This function is used to read card data. If the reading is successful, id and te
This function is used to write information to the card, press ``Enter`` key to finish writing. ``text`` is the information to be written to the card.

Phenomenon Picture
------------------
---------------------

.. image:: img/image233.jpeg
3 changes: 2 additions & 1 deletion docs/source/appendix/appendix.rst
Expand Up @@ -7,4 +7,5 @@ Appendix
i2c_configuration
spi_configuration
remote_desktop
get_ip
get_ip
create_virtual_environment
109 changes: 109 additions & 0 deletions docs/source/appendix/create_virtual_environment.rst
@@ -0,0 +1,109 @@
.. _create_virtual:

Creating a Virtual Environment
======================================
When using Raspberry Pi or similar devices, it is recommended to install packages with ``pip`` in a virtual environment. It offers dependency isolation, increases system security, maintains system cleanliness, and facilitates project migration and sharing, simplifying dependency management. These benefits make virtual environments an extremely important and useful tool in Python development.

Below are the steps to create a virtual environment:

**1. Create a virtual environment**

Firstly, you need to ensure that your system has Python installed. Python version 3.3 and later come with the ``venv`` module to create virtual environments, eliminating the need for separate installation. If you are using Python 2 or a version before Python 3.3, you will need to install ``virtualenv``.

* For Python 3:

Python 3.3 and later versions can directly use the ``venv`` module:

.. raw:: html

<run></run>

.. code-block:: shell
python3 -m venv myenv
This will create a virtual environment named ``myenv`` in the current directory.

* For Python 2:

If you are still using Python 2, you first need to install ``virtualenv``:

.. raw:: html

<run></run>

.. code-block:: shell
pip install virtualenv
Then, create a virtual environment:

.. raw:: html

<run></run>

.. code-block:: shell
virtualenv myenv
This also creates a virtual environment named ``myenv`` in the current directory.

**2. Activating the Virtual Environment**

After creating the virtual environment, you need to activate it for use.

.. note::

Each time you restart the Raspberry Pi, or open a new terminal, you will need to run the following command again to activate the virtual environment.

.. raw:: html

<run></run>

.. code-block:: shell
source myenv/bin/activate
Once the virtual environment is activated, you will see the environment name before the command line prompt, indicating you are working within the virtual environment.


**3. Installing Dependencies**

With the virtual environment activated, you can use pip to install the required dependencies. For example:

.. raw:: html

<run></run>

.. code-block:: shell
pip install requests
This will install the requests library into the current virtual environment, rather than the global environment. This step only needs to be done once.


**4. Exiting the Virtual Environment**

When you have completed your work and wish to exit the virtual environment, simply run:

.. raw:: html

<run></run>

.. code-block:: shell
deactivate
This will return you to the system's global Python environment.

**5. Deleting the Virtual Environment**

If you no longer need a particular virtual environment, you can simply delete the directory containing the virtual environment:

.. raw:: html

<run></run>

.. code-block:: shell
rm -rf myenv
41 changes: 39 additions & 2 deletions docs/source/appendix/i2c_configuration.rst
Expand Up @@ -103,12 +103,49 @@ If there is an I2C device connected, the address of the device will be displayed
sudo apt-get install libi2c-dev
**For Python users:** Install smbus for I2C.
**For Python users:**

1. Activating the Virtual Environment.

.. note::

* Before activation, you need to ensure that you have created a virtual environment, please refer to: :ref:`create_virtual`.

* Each time you restart the Raspberry Pi, or open a new terminal, you will need to run the following command again to activate the virtual environment.

.. raw:: html

<run></run>

.. code-block:: shell
source myenv/bin/activate
Once the virtual environment is activated, you will see the environment name before the command line prompt, indicating you are working within the virtual environment.



2. Install smbus for I2C.

.. raw:: html

<run></run>

.. code-block::
sudo pip3 install smbus2
sudo pip3 install smbus2
3. Exiting the Virtual Environment.

When you have completed your work and wish to exit the virtual environment, simply run:

.. raw:: html

<run></run>

.. code-block:: shell
deactivate
This will return you to the system's global Python environment.

0 comments on commit 346aa2e

Please sign in to comment.