关于python:为什么conda安装tk在我的docker容器中不起作用,即使它说它已安装?

Why does conda install tk not work in my docker container even though it says its installed?

我在我的python 3 docker容器中遇到了tk的问题。

我试过了:

1
conda install tk

但它说它确实安装了它

1
2
3
4
5
6
7
8
root@36602e2cd649:/home_simulation_research/overparametrized_experiments/pytorch_experiments# conda install tk
Fetching package metadata ...........
Solving package specifications: .

# All requested packages already installed.
# packages in environment at /opt/conda:
#
tk                        8.6.7                h5979e9b_1

但是当我去python并尝试导入它时它不起作用:

1
2
3
4
>>> import Tkinter
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
ImportError: No module named 'Tkinter'

和其他错误:

1
2
3
4
5
6
>>> import tkinter
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libX11.so.6: cannot open shared object file: No such file or directory

当我运行需要它的脚本时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Traceback (most recent call last):
  File"bulk_experiment_dispatcher.py", line 18, in <module>
    from data_file import *
  File"/home_simulation_research/overparametrized_experiments/pytorch_experiments/data_file.py", line 16, in <module>
    import matplotlib.pyplot as plt
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File"/opt/conda/envs/pytorch-py35/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libX11.so.6: cannot open shared object file: No such file or directory

我尝试了apt-get install python-tk(来自安装tkinter for Python),但它不起作用:

1
2
3
4
5
root@36602e2cd649:/home_simulation_research/overparametrized_experiments/pytorch_experiments# apt-get install python-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python-tk

我尝试运行ENTERYPOINT作为建议的答案之一,但它又给我一些错误:

1
2
3
4
/path/fake_gui.sh: 8: /home_simulation_research/overparametrized_experiments/docker_files/runtime/fake_gui.sh: source: not found
/path/fake_gui.sh: 12: /home_simulation_research/overparametrized_experiments/docker_files/runtime/fake_gui.sh: function: not found
/path/fake_gui.sh: 13: kill: invalid signal number or name: SIGTERM
/path/fake_gui.sh: 15: /home_simulation_research/overparametrized_experiments/docker_files/runtime/fake_gui.sh: Syntax error:"}" unexpected

但不知道该怎么办......

有用的问题:

如何在我的docker镜像中安装python-tk


好吧,所以一旦我在图像中放置一个虚拟屏幕,它就会停止崩溃:

1
2
3
RUN apt-get update
RUN apt-get install -y xvfb
#RUN Xvfb :1 -screen 0 1024x768x16 &> xvfb.log  &

当我运行我的码头图像。


您需要使用已安装并运行虚拟帧缓冲区的Docker容器。

这篇博客文章解释了将X11放入docker容器的"为什么和如何"。

您可以通过entry_point.sh了解他们如何在Selenium Docker容器中执行此操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
#
# IMPORTANT: Change this file only in directory Standalone!

source /opt/bin/functions.sh

export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"

function shutdown {
  kill -s SIGTERM $NODE_PID
  wait $NODE_PID
}

if [ ! -z"$SE_OPTS" ]; then
  echo"appending selenium options: ${SE_OPTS}"
fi

SERVERNUM=$(get_server_num)

rm -f /tmp/.X*lock

xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
  java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
  ${SE_OPTS} &
NODE_PID=$!

trap shutdown SIGTERM SIGINT
wait $NODE_PID

上面代码的源代码来自GitHub上的这个存储库,SeleniumHQ / docker-selenium。


使用python 3,您必须导入如下:

1
import tkinter   # with a small caps 't'