I'm trying to run a simple python script that takes a json string as an argument in a docker container. However I get the following error:
Traceback (most recent call last):
File "/root/simple.py", line 2, in <module>
import json
ImportError: No module named json
I'm running the standard ubuntu:12.04 image. Here's how I call up the container:
docker run -v $(pwd)/:/root/ ubuntu:12.04 python /root/simple.py '[{"hi":"bye"}]'
My simple.py script is just:
import sys
import json
configs = json.loads(sys.argv[1])
print configs
def read_option_keys(json_file):
json_file[0]["new"] = None
print json.dumps(json_file)
read_option_keys(configs)
Any idea why it's not returning the following as expected:
[{u'hi': u'bye'}]
[{"hi": "bye", "new": null}]
I was able to solve the issue myself. Ubuntu image is super bare-bones. I pulled the dockerfile/python image and now it works.
Docker is a containerized instance, so your python script has no knowledge of the outside world. You are currently running the Ubuntu image, but it doesn't contain the python standard module. You should run a python image instead or add python to your Ubuntu image
Related
I'm trying to run a python script containing submodules from Github to finetune its model.
The final step of the process is to invoke
python realesrgan/train.py -opt options/finetune_realesrgan_x4plus_pairdata.yml --auto_resume
However, doing from root of repo (and from realesrgan folder for that matter) so results in:
Traceback (most recent call last):
File "D:\Real-ESRGAN\realesrgan\train.py", line 5, in
import realesrgan.archs
ModuleNotFoundError: No module named 'realesrgan'
Yet, by launching python interactively from repo root (just "python") and then invoking
import realesrgan.archs
results in me being able to import it without any problem. All mentioned folders have required init.py in them.
This is my first time seeing discrepancy between calling a script and interactive python and I'm wondering, what can cause this.
System specifics just in case:
Windows 11
Python 3.9.9
python script execution fails from either powershell/cmd/file explorer
I'm trying to run a python script saved on my local system in node-red which is running as a docker container. I copied the python script into the docker container as the exec node was unable to locate the file using this command -
cat /local/file/path | docker exec -i <running-container-id> sh -c 'cat > /inside/docker/file/path'
But now I'm getting the following error - Traceback (most recent call last):
File "outlier.py", line 2, in
from pandas import read_csv
ModuleNotFoundError: No module named 'pandas'
I had installed pandas on my local but it's not being found by the exec node. Any help is appreciated, thanks.
When applications run inside a Docker container they only have access to the libraries/modules included inside the container. They have no access to anything in the host machine.
So if you want to run Python scripts that have dependencies on Python modules you will need to create a custom Docker container that extends the official Node-RED container and then installs those modules.
Node-RED provides doc about extending it's container here
I am completely new to docker (on windows 10 machine). I intend to setup a python development environment as a docker container. And most of the reading that I did involved the use of Dockerfile. I want to do it from scratch instead purely using commands.
What I intend to do is very basic requirement: To have python docker image present with me and that I should be able to install more libraries in that image and commit these updates to that image. But I want to do it completely using commands (not via a Dockerfile).
I am using Docker Desktop on windows 10 machine. I did docker pull python:latest and it pulled the image like so:
C:\Users\MyHomeDirectory>docker pull python:latest
latest: Pulling from library/python
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
532f6f723709: Pull complete
1334e0fe2851: Pull complete
062ada600c9e: Pull complete
aec2e3a89371: Pull complete
1ec7c3bcb4b2: Pull complete
Digest: sha256:65367d1d3eb47f62127f007ea1f74d1ce11be988044042ab45d74adc6cfceb21
Status: Downloaded newer image for python:latest
docker.io/library/python:latest
Then I did docker images and it showed that python latest image is present with a size of 886 MB.
C:\Users\Tejas.Khajanchee>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest 5b3b4504ff1f 47 hours ago 886MB
I am also able to enter the interactive python by doing docker run -it python and it generates the interactive shell:
Python 3.9.5 (default, May 12 2021, 15:26:36)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import gc
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>>
>>> import openpyxl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'openpyxl'
>>>
But as evident, some of the libraries are not installed. But this is where I get stuck. How do I install libraries into the python image and have the image updated. Also, if this shell is the only thing that I am allowed to do till now, what does the 886 MB content represent? Also I want to be able to run scripts using this docker image. When I attempt to do this on a very basic hello world script, the following error comes up:
C:\Users\MyHomeDirectory\Downloads>docker run -it python a.py
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "a.py": executable file not found in $PATH: unknown.
I want to be able to do this purely with commands and not a Dockerfile. Please help.
First, looks you confuse the concept of image & container.
Docker image: read only, used as basis of container
Docker container: overlay a writeable layer upon the read only layer of docker image, all container will use image as basis
Second, for you, you mentioned you want to install numpy in the image, the best way for this is to customized a Dockerfile like next:
Dockerfile:
FROM python
RUN pip install numpy
Then, build a new image with docker build -t newpython .
BUT, you mentioned you don't want to use Dockerfile, then the replacement is next:
Install numpy in a container:
docker run -it python /bin/bash
# pip install numpy
Use docker ps -a to get the container id, e.g: 0a6b4df8e2c2, then commit this container which already have numpy installed to a new image:
docker commit 0a6b4df8e2c2 newpython
Finally, all new container need to run base on newpython image not python image, as only the newpython image has numpy installed:
docker run --rm newpython python -c "import numpy; print(numpy.__version__)"
1.20.3
Additional, for docker run -it python a.py, I think you misunderstand the concept. Container command like python a.py means the command will executed in container, so the a.py should be in container, not in host machine.
I created a docker container of a Python program and I need to run it on a virtual machine.
I prepared a Dockerfile to create the container, where I launch the main script by using:
CMD python3 /home/project_name_folder/script_00.py
built it as usual:
docker build -t registry_address/image_name -f folder_name/Dockerfile_01 .
and pushed the image to a registry. Then, I pulled and ran the image on the VM by:
docker run -p 5555:8050 registry_address/image_name
The problem is that, on the VM, I'm getting an error which never occurs when I run a container generated from the same image on my PC.
The error looks something like this:
Traceback (most recent call last):
File "/home/project_name_folder/script_02.py", line 188, in <module>
infile = open(sys.argv[1], 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'temp/3004601723038773'
So, script_00.py is executed, creates the file temp/3004601723038773, and calls another script_02 passing the file name as an argument. However, the container on the VM seems to be unable to find the corresponding file.
What surprises me is that this is never happening when running the container on my local computer, so I assume it's not due to the code itself. Can it be related to the docker version, or something similar?
P.S. sorry if the terminology I'm using is not correct, I'm a beginner when it comes to Docker!
I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.