PyCharm use it to configure SSH Interpreter

  In the article the use of the image using the Docker build Python development environment PyCharm , this article describes how to use the built in PyCharm Docker mirrored in Python development environment. In this article, we will explain how to use PyCharm to configure SSH Interpreter, this article SSH Interpreter with Docker mirror to achieve.
  Demonstrations of the structure shown below:

Project Screenshot

Docker mirror making services with SSH

  First, let's make Docker mirror with SSH service, which contains Python development environment, Dockerfile file python_env.build, reads as follows:

FROM centos:7.2.1511

# author label
LABEL maintainer="jclian"

# install related packages
ENV ENVIRONMENT DOCKER_PROD
RUN cd / && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum makecache \
    && yum install -y wget aclocal automake autoconf make gcc gcc-c++ python-devel mysql-devel bzip2 libffi-devel epel-release \
    && yum clean all

# install python 3.7.0
RUN wget https://npm.taobao.org/mirrors/python/3.7.0/Python-3.7.0.tar.xz \
    && tar -xvf Python-3.7.0.tar.xz -C /usr/local/ \
    && rm -rf Python-3.7.0.tar.xz \
    && cd /usr/local/Python-3.7.0 \
    && ./configure && make && make install

# 创建src目录
COPY src /root/src
WORKDIR /root/src

# install related packages
RUN pip3 install -i https://pypi.doubanio.com/simple/ -r requirements.txt

# expose port
EXPOSE 15731

# install ssh
RUN yum -y update; yum clean all
RUN yum -y install openssh-server passwd; yum clean all
ADD ./start.sh /start.sh
RUN mkdir /var/run/sshd

RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''

RUN chmod 755 /start.sh
RUN /start.sh
ENTRYPOINT ["/usr/sbin/sshd", "-D"]

In this CentOS system, we configured the SSH service, which contains the account name and password in start.sh, yet fully equipped with the Python development environment, start.sh file contents are as follows:

#!/bin/bash

__create_user() {
# Create a user to SSH into as.
useradd user
SSH_USERPASS=newpass
echo -e "$SSH_USERPASS\n$SSH_USERPASS" | (passwd --stdin user)
echo ssh user password: $SSH_USERPASS
}

# Call all functions
__create_user

requirements.txt reads as follows:

tornado==5.1.1
jieba==0.39

Installation of third-party modules for the tornado and jieba. To mirror script build.sh, reads as follows:

#!/usr/bin/env bash

TIMENOW=`date +%y.%m.%d.%H%M`

# 进行docker镜像打包
docker build -f python_dev.build -t ssh_docker_python_dev:${TIMENOW} .

Run the mirror, the mirror is generated docker ssh_docker_python_dev: 19.12.25.1346, as follows:

$ docker images | grep ssh_docker
ssh_docker_python_dev   19.12.25.1346   cca5b78c199c    9 hours ago 988MB

This, production docker mirror finished.

Test mirrored SSH service

  Then, we start the docker mirrored locally, the command is as follows:

$ docker run -d -p 49154:22 ssh_docker_python_dev:19.12.25.1346

Then enter:

$ ssh -p 49154 [email protected]

Select yes when accepting credentials and enter the password "newpass", will be able to successfully enter the interior of the container, and try to use Python as follows:

$ ssh -p 49154 [email protected]
[email protected]'s password:
[user@4718f33bc470 ~]$ python3
Python 3.7.0 (default, Dec 24 2019, 23:58:12)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jieba
>>> list(jieba.cut("三亚的风景很不错"))
Building prefix dict from the default dictionary ...
Dumping model to file cache /tmp/jieba.cache
Loading model cost 1.197 seconds.
Prefix dict has been built succesfully.
['三亚', '的', '风景', '很', '不错']

Configuring the SSH Interpreter in PyCharm

  In the System Configuration PyCharm, the "Project Interpreter" selection can be seen in the "Project Inter" is not the right of the interpreter, we click on the small gear are subject to change, select "Add", as shown below:

New interpreter
In the pop up screen, we selected interpreter is of type "SSH Interpreter", while link information is provided below:
SSH Interpreter configuration

Click on "Next", password "newpass", and then click on "Next", select the path for the Interpreter "/usr/local/Python-3.7.0/python", as shown below:

Python path disposed in the container
Note that here we can see the local project path corresponding to the container / tmp / pycharm_project_951 path, and the project file will be automatically uploaded to the project path corresponding container. Finally, click "Finish" to complete the SSH Interpreter configuration.

Testing SSH Interpreter

  After configuring the Python environment, we jieba_test.py as a test file to verify Python environment is configured. jieba_test.py code is as follows:

# -*- coding: utf-8 -*-
# author: Jclian91
# place: Pudong Shanghai
# time: 2:09 下午
import os, re, json, traceback
import jieba

if __name__ == '__main__':
    sent = '最近这几天,我都在三亚上班,看着海开发的感觉还不错。'
    print(list(jieba.cut(sent)))

Output:

ssh://[email protected]:49154/usr/local/Python-3.7.0/python -u /tmp/pycharm_project_58/jieba_test.py
Building prefix dict from the default dictionary ...
Loading model from cache /tmp/jieba.cache
Loading model cost 0.775 seconds.
Prefix dict has been built succesfully.
['最近', '这', '几天', ',', '我', '都', '在', '三亚', '上班', ',', '看着', '海', '开发', '的', '感觉', '还', '不错', '。']

Great, Python development environment in Docker container can be used. At this time, we enter the Docker container to view the next, as follows:

$ docker ps
CONTAINER ID        IMAGE                                 COMMAND               CREATED             STATUS              PORTS                              NAMES
4718f33bc470        ssh_docker_python_dev:19.12.25.1346   "/usr/sbin/sshd -D"   20 minutes ago      Up 20 minutes       15731/tcp, 0.0.0.0:49154->22/tcp   brave_nobel
$ docker exec -it 4718f33bc470 bash
[root@4718f33bc470 src]# cd /tmp/pycharm_project_58/
[root@4718f33bc470 pycharm_project_58]# ls -l
total 24
-rw-rw-r-- 1 user user  960 Dec 25 14:12 README.md
-rw-rw-r-- 1 user user  150 Dec 25 13:21 build.sh
-rw-rw-r-- 1 user user  284 Dec 25 14:10 jieba_test.py
-rw-rw-r-- 1 user user 1100 Dec 25 13:46 python_dev.build
drwxrwxr-x 2 user user 4096 Dec 25 22:48 src
-rw-rw-r-- 1 user user  235 Dec 25 13:21 start.sh
[root@4718f33bc470 pycharm_project_58]#

It can be seen at the container / tmp / pycharm_project_58 path does contain all the files for local projects.

  Docker on how to make a mirror with SSH services, and Python development environment and how to configure SSH Interpreter tells the PyCharm in. Thank you for reading ~
  The demonstration project has been uploaded to Github, available at: https: //github.com / percent4 / Python_docker.

注意: Micro-channel public may wish to know the number of the next author: Python crawlers and algorithms (Micro Signal as: easy_web_scrape), welcome attention ~

Guess you like

Origin www.cnblogs.com/jclian91/p/12099549.html