Docker, Python, and "import logging" - python-3.x

I have trying to build a docker image/container that is Python 3.8.2 and that will run a specific script when ran. I can get it to build but when I try and run it breaks.
Specifically, I am running into a problem where:
...
import grpc
...
gives the error:
Traceback (most recent call last):
File "/project1/script1.py", line 4, in <module>
import grpc
File "/usr/local/lib/python3.8/site-packages/grpc/__init__.py", line 23, in <module>
from grpc._cython import cygrpc as _cygrpc
File "src/python/grpcio/grpc/_cython/cygrpc.pyx", line 27, in init grpc._cython.cygrpc
File "/usr/local/lib/python3.8/asyncio/__init__.py", line 8, in <module>
from .base_events import *
File "/usr/local/lib/python3.8/asyncio/base_events.py", line 18, in <module>
import concurrent.futures
File "/usr/local/lib/python3.8/concurrent/futures/__init__.py", line 8, in <module>
from concurrent.futures._base import (FIRST_COMPLETED,
File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 42, in <module>
LOGGER = logging.getLogger("concurrent.futures")
AttributeError: module 'logging' has no attribute 'getLogger'
HOWEVER,
if i use the
docker run -it <image name> sh
to run the image, if I try to run pip in the Docker containers command line I get the same error.
Here is the Dockerfile used to create the image:
FROM python:3.8.2
#Build the Python Environments
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt
RUN pip install "obspy==1.2.1"
#COPY project1
COPY project1 /
#Update PYTHON PATH to include project1
ENV PYTHONPATH "${PYTHONPATH}:/project1"
#Run the script1.py script
CMD python /project1/script1.py
All of this runs fine on my windows box in a Python 3.8.2 environment that contains all the packages in requirements.txt
Anyone have any suggestions on what is going on?

May be try with a different python version image.

Try mentioning the specific version of the dependency in requirements.txt file

Is there any python file named logging.py or module folder named logging in your project? If so try give it another name.

Related

class YAMLObject(metaclass=YAMLObjectMetaclass): invalid syntax for docker-compose up command

On my Linux system two python versions are present 2.7 and 3.6. To use python 3.6. in my PYTHONPATH in .profile file I have kept /usr/local/lib64/python3.6/site-packages and /usr/local/lib/python3.6/site-packages at the top. and there is no entry of python 2.7 path. In my sys.path variable I can see the python2.7 path entries. (dont know how they appear)
now the issue is when I use docker-compose up command it gives me below error. docker-compose version is 1.29.2
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 7, in <module>
from compose.cli.main import main
File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 22, in <module>
from ..bundle import get_image_digests
File "/usr/lib/python2.7/site-packages/compose/bundle.py", line 12, in <module>
from .config.serialize import denormalize_config
File "/usr/lib/python2.7/site-packages/compose/config/__init__.py", line 6, in <module>
from .config import ConfigurationError
File "/usr/lib/python2.7/site-packages/compose/config/config.py", line 13, in <module>
import yaml
File "/usr/local/lib64/python3.6/site-packages/yaml/__init__.py", line 284
class YAMLObject(metaclass=YAMLObjectMetaclass):
^
SyntaxError: invalid syntax
If I add python 2.7 site package path at the top of PYTHONPATH docker-compose works but other programs start failing. I dont want to use python 2.7 how can I use only python 3.6.
Please suggest.

Packages doesn't set up on python venv

I'm trying to build pyslam project, and the install.sh file was provided. But when I executed install.sh, the main.py didn't work.
https://github.com/luigifreda/pyslam#install-pyslam-under-ubuntu-2004
Full Installation
In order to run main_slam.py with venv, get in the root of this repository and run the following command:
$ . install_all_venv.sh
N.B.: do not forget the dot! (without '/' !)
This will compile the required thirdparty packages and will also activate the created pyslam environment. Now, from the same terminal, you can run:
$ python3 -O main_slam.py
The result was
(pyslam) (base) ict-526#ict526-MS-7D32:~/pyslam$ python3 -O main_slam.py
Traceback (most recent call last):
File "/home/ict-526/pyslam/main_slam.py", line 27, in <module>
from config import Config
File "/home/ict-526/pyslam/config.py", line 27, in <module>
import yaml
ModuleNotFoundError: No module named 'yaml'
I think the packages didn't install in the venv (pyslam). How can I fix this situation?
My environment is ubuntu20.04, and python 3.9.7.
According to the installaion instruction, python 3.6.9 was required. After install_all_venv.sh, the pyslam venv support python 3.9.7...

AWS CLI in CMD and PowerShell

On Win 10 I've installed Anaconda (via chocolatey) and the AWS CLI. I can successfully issue commands via the AWS CLI as long as I'm in one of the following:
an Anaconda prompt
a Git Bash prompt
the powershell terminal inside of visual studio code
However, when I try to issue commands from a "straight-up" CMD or PowerShell window, then I get errors:
C:\>aws s3 ls
Traceback (most recent call last):
File "c:\Users\barias\AppData\Roaming\Python\Python37\Scripts\aws.cmd", line 50, in <module>
import awscli.clidriver
File "C:\Users\barias\AppData\Roaming\Python\Python37\site-packages\awscli\clidriver.py", line 17, in <module>
import botocore.session
File "C:\tools\Anaconda3\lib\site-packages\botocore\session.py", line 30, in <module>
import botocore.credentials
File "C:\tools\Anaconda3\lib\site-packages\botocore\credentials.py", line 34, in <module>
from botocore.config import Config
File "C:\tools\Anaconda3\lib\site-packages\botocore\config.py", line 16, in <module>
from botocore.endpoint import DEFAULT_TIMEOUT, MAX_POOL_CONNECTIONS
File "C:\tools\Anaconda3\lib\site-packages\botocore\endpoint.py", line 22, in <module>
from botocore.awsrequest import create_request_object
File "C:\tools\Anaconda3\lib\site-packages\botocore\awsrequest.py", line 26, in <module>
import botocore.utils
File "C:\tools\Anaconda3\lib\site-packages\botocore\utils.py", line 31, in <module>
import botocore.httpsession
File "C:\tools\Anaconda3\lib\site-packages\botocore\httpsession.py", line 7, in <module>
from urllib3.util.ssl_ import (
ImportError: cannot import name 'ssl' from 'urllib3.util.ssl_' (C:\tools\Anaconda3\lib\site-packages\urllib3\util\ssl_.py)
I am not a Python programmer, so it is not self-evident what this error means.
A side commentary on my Anaconda install:
I am surprised to see Anaconda installed in C:\tools. I think that is because (1) my chocolatey script which installed Anaconda was executed inside that folder and (2) the Anaconda chocolatey package perhaps was designed to install in the current working directory? Regardless, I don't think this problem is a result of where Anaconda is installed.
I also found that I had to manually put the python exe onto the path. I'm not sure why the Anaconda install didn't do that for me.
As it turns out, I can successfully issue terraform commands from CMD or PowerShell to that same AWS account. I would have thought if the AWS CLI wasn't working, then terraform would be hosed as well. Weird.
None of the prompts (Git Bash, Anaconda, CMD, PowerShell) have PYTHONPATH OR PYTHONHOME environment variables set. The Anaconda path has these environment variables:
CONDA_DEFAULT_ENV=base
CONDA_EXE=C:\tools\Anaconda3\Scripts\conda.exe
CONDA_PREFIX=C:\tools\Anaconda3
CONDA_PROMPT_MODIFIER=(base)
CONDA_PYTHON_EXE=C:\tools\Anaconda3\python.exe
CONDA_SHLVL=1
I tried setting these in the CMD window, but it did not fix the problem. Also, the Git Bash prompt successfully handles the AWS CLI...and it doesn't have any environment variables configured regarding python or "conda".
Any ideas of why I'm getting this error? Remember, it works fine as long as I execute commands from an Anaconda prompt.

Tensorflow Object Detection API / ImportError: cannot import name 'keypoint_box_coder_pb2'

I followed instructions (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) to install the Tensorflow Object Detection
API. I use Anaconda on Windows 10 and all of the steps in the instructions seemed to complete OK.
But when trying to run python object_detection/builders/model_builder_test.py to test the installation, I get: ImportError: cannot import name 'keypoint_box_coder_pb2'
What am I missing?
(tensorflow) C:\Users\User\Documents\GitHub\models\research>python
object_detection/builders/model_builder_test.py Traceback (most recent
call last): File "object_detection/builders/model_builder_test.py",
line 21, in
from object_detection.builders import model_builder File "C:\Users\User\New\Anaconda3\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\builders\model_builder.py",
line 18, in
from object_detection.builders import box_coder_builder File "C:\Users\User\New\Anaconda3\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\builders\box_coder_builder.py",
line 21, in
from object_detection.protos import box_coder_pb2 File "C:\Users\User\New\Anaconda3\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\protos\box_coder_pb2.py",
line 17, in ImportError: cannot import name
'keypoint_box_coder_pb2'
I got it to work. What I did was:
I ran:
protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto
What was missing was: .\object_detection\protos\keypoint_box_coder.proto
Then ran:
python setup.py build
python setup.py install
Then changed:
nets to research.slim.net in faster_rcnn_inception_resnet_v2_feature_extractor.py and faster_rcnn_resnet_v1_feature_extractor.py (in the models folder)
Ran:
python object_detection/builders/model_builder_test.py
Got:
Ran 11 tests in 0.195s
OK
It looks like you have not run
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim # (from tensorflow/models/research folder)
in the same terminal before launching the test, have you ? If you've chosen the .bashrc option, you probably need to reload your bshrc filewith . ~/.bashrc

Python3.4 - Install pyautogui - Raspberry Pi 3

I've been trying to install pyautogui for Python 3.4 on my Raspberry Pi 3 for the last 2 hours... I need it because I wrote a touch keyboard application that needs to run on it. On my main machine (elementary OS 0.4 with Python 3.5) I was able to install it without any problems. First I tried sudo pip install pyautogui but that only installed the Python 2.7 version of pyautogui. Of course next I tried sudo pip3 install pyautogui but that gave me this error:
Running setup.py (path:/tmp/pip-build-hf289jja/pyautogui/setup.py) egg_info for package pyautogui
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/tmp/pip-build-hf289jja/pyautogui/setup.py", line 6, in <module>
version=__import__('pyautogui').__version__,
File "/tmp/pip-build-hf289jja/pyautogui/pyautogui/__init__.py", line 114, in <module>
from . import _pyautogui_x11 as platformModule
File "/tmp/pip-build-hf289jja/pyautogui/pyautogui/_pyautogui_x11.py", line 6, in <module>
from Xlib.display import Display
File "/usr/local/lib/python3.4/dist-packages/Xlib/display.py", line 20, in <module>
import new
ImportError: No module named 'new'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 17, in
File "/tmp/pip-build-hf289jja/pyautogui/setup.py", line 6, in
version=__import__('pyautogui').__version__,
File "/tmp/pip-build-hf289jja/pyautogui/pyautogui/init.py", line 114, in
from . import _pyautogui_x11 as platformModule
File "/tmp/pip-build-hf289jja/pyautogui/pyautogui/_pyautogui_x11.py", line 6, in
from Xlib.display import Display
File "/usr/local/lib/python3.4/dist-packages/Xlib/display.py", line 20, in
import new
ImportError: No module named 'new'
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-hf289jja/pyautogui
Storing debug log for failure in /root/.pip/pip.log
... sorry for the formatting of the above, I couldn't get it to stay inside one code box.
I have no idea what module "new" is. I tried everything else under the sun, such as installing from the official resources: https://pypi.python.org/pypi/PyAutoGUI and various other guides, with NO results.. I'm kinda loosing my mind right now.
ANY Help is grately appreciated :)
Try the following:
sudo pip3 install python3-xlib
sudo pip3 install pyautogui
This assumes you have some other python3 libraries already installed, but you will see missing installs in the above if not.

Resources