简体   繁体   中英

I cannot import a module to Python 3.7 since it is directed to python2.7

I'm new to coding. I was given a raspberry pi 4 for Christmas and I am trying to use it to make an automated sprinkler system. My problem is that I am unable to send the data from the capacitive soil sensor through my ESP32 to the Pi. I have looked at several online sources and have tried a few different methods to get my ESP32 to send the data to Pi without having any positive results. When using the lines of code below I received an error.

import time
import machine
adc = machine.ADC(machine.Pin(34))
print(adc.read())

Traceback (most recent call last): File "/home/pi/Documents/esp32 test.py", line 2, in import machine ModuleNotFoundError: No module named 'machine'

A few people on stack overflow have had similar problems with installing modules and I've tried following with pip and pip3 however these haven't moved my installation from the python2.7 directory to python3.7. I am using python3.7 because the Thonny IDE uses it and I think that the program can't find the "module named 'machine'" because it's saved to python2.7. I also tried copying the 'machine-0.0.1.dist-info' from the dist-packages of 2.7 to 3.7 but found this:

machine-0.0.1.dist-info: Error creating directory /usr/local/lib/python3.7/dist-packages/machine-0.0.1.dist-info: Permission denied

I would appreciate if someone would share their knowledge on how to move the 'machine' module to python3.7 or present a different way to get a raspberry pi 4 to communicate with an ESP32. Thanks.

You can specify versions in either partial or full form in order to install then to your desired python versions.

If you want to select versions per command, you can use command-line options like these: py -3 -m pip install packagename (for 3.7) py -2 -m pip install packagename (for 2.7)

This should work well if python is installed properly.

The Python code you posted seems to be MicroPython code that's intended to run on the ESP32, not on your Raspberry Pi:

https://docs.micropython.org/en/latest/esp32/quickref.html

Note
Do not try to run pip install machine on your Raspberry Pi. It will not download the right package. Instead, it'll install this package: https://pypi.org/project/machine/ which is not at all what you need.


That being said:
When installing packages using pip, you probably don't want to install them system-wide, as this requires root permissions, which is bad practice and could be dangerous, because you're giving unknown code full administrator control over your system.

There are two alternatives:

1. Use a virtual environment

Read more here: https://docs.python.org/3/library/venv.html

Basically:

python3.7 -m venv ~/myvenv
source ~/myvenv/bin/activate
pip install <package>

To work on your project, you'll have to activate the virtual environment. Any decent IDE has options to do this automatically.
This is the cleanest solution, because it keeps packages for different projects separate.

2. Install the package at user level

This installs the packages in ~/.local/lib/python3.7/site-packages which is in your home directory, so it doesn't require root permissions.

To make sure that you're installing a package for the right Python version, you can run pip from Python, instead of running pip install directly.

python3.7 -m pip install --user <package>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM