Ubuntu16.04下搭建python开发环境

  • Post author:
  • Post category:python




准备工作:安装虚拟机,并下载安装Ubuntu16.04系统



1.ubuntu默认root用户没有激活,激活root用户,就要为root用户创建密码


$sudo passwd root


2、修改主机名


$vi /etc/hostname


3、安装ssh服务


$sudo apt-get install openssh-server


//安装sshd服务
$ sudo apt-get install openssh-server
//开启服务
$ /etc/init.d/ssh start
//关闭服务
$ /etc/init.d/ssh stop
//重启服务
$ /etc/init.d/ssh restart


4、安装Xshell软件,链接虚拟机ubuntu


5、浏览器访问虚拟机里的web server


首先你的虚拟机应该与你的win系统网络访问正常



启动web Server时确认其启动在对外能够访问的IP( 0.0.0.0 ) 和端口上


6、安装python2.7




$ sudo apt-get install python2.7
$ which python2.7
$ which python3
$ python --version

Arvin@Arvin:~/tools/pip$ which python2.7
/usr/bin/python2.7

Arvin@Arvin:~/tools/pip$ python2.7
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


创建软连接:


$ ln -s 源文件 目标文件

$ sudo ln -s /usr/bin/python2.7 /usr/bin/python

lrwxrwxrwx 1 root root 18 Mar 28 23:23 /usr/bin/python -> /usr/bin/python2.7
lrwxrwxrwx 1 root root 18 Mar 28 23:22 /usr/bin/python2 -> /usr/bin/python2.7
-rwxr-xr-x 1 root root 3546104 Nov 19 01:35 /usr/bin/python2.7
lrwxrwxrwx 1 root root 9 Mar 28 03:22 /usr/bin/python3 -> python3.5
-rwxr-xr-x 2 root root 4460336 Nov 17 11:23 /usr/bin/python3.5


7、安装setuptools


$ sudo apt-get install python-setuptools


8、安装pip


Arvin@Arvin:~/tools/pip$ wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate

--2017-03-28 22:44:39-- https://bootstrap.pypa.io/get-pip.py
Resolving bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.72.175
Connecting to bootstrap.pypa.io (bootstrap.pypa.io)|151.101.72.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1595408 (1.5M) [text/x-python]
Saving to: ‘get-pip.py’

get-pip.py 100%[===================>] 1.52M 933KB/s in 1.7s 

2017-03-28 22:44:41 (933 KB/s) - ‘get-pip.py’ saved [1595408/1595408]


执行安装pip命令


Arvin@Arvin:~/tools/pip$ sudo python get-pip.py


警告解决办法


DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.


新建pip源配置文件


$ mkdir ~/.pip
$ vim ~/.pip/pip.conf


添加内容



[list]
format=columns


pip源配置文件可以放置的位置


Linux/Unix:
/etc/pip.conf
~/.pip/pip.conf
~/.config/pip/pip.conf


9、安装virtualenv和virtualenvwrapper


sudo apt-get update

$sudo apt-get install python-virtualenv
$sudo easy_install virtualenvwrapper


上述工具装好后找不到mkvirtualenv命令,需要执行以下环境变量设置


1.创建目录用来存放虚拟环境


mkdir $HOME/.virtualenvs



2.在~/.bashrc中添加行:



export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh


3.运行:


source ~/.bashrc


3.创建python虚拟环境


mkvirtualenv [虚拟环境名称]


4.切换虚拟环境



workon [虚拟环境名称]


5.退出虚拟环境



deactivate


6.删除虚拟环境



rmvirtualenv [虚拟环境名称]



注:创建的环境是独立的,互不干扰,无需sudo权限即可使用 pip 来进行包的管理,如果在虚拟环境中使用sudo安装的包在主环境中


使用-p参数指定虚拟环境中python的版本


~/.bashrc 文件内容



1 # ~/.bashrc: executed by bash(1) for non-login shells.
2 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
3 # for examples
4
5 # If not running interactively, don't do anything
6 case $- in
7 *i*) ;;
8 *) return;;
9 esac
10
11 # don't put duplicate lines or lines starting with space in the history.
12 # See bash(1) for more options
13 HISTCONTROL=ignoreboth
14
15 # append to the history file, don't overwrite it
16 shopt -s histappend
17
18 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
19 HISTSIZE=1000
20 HISTFILESIZE=2000
21
22 # check the window size after each command and, if necessary,
23 # update the values of LINES and COLUMNS.
24 shopt -s checkwinsize
25
26 # If set, the pattern "**" used in a pathname expansion context will
27 # match all files and zero or more directories and subdirectories.
28 #shopt -s globstar
29
30 # make less more friendly for non-text input files, see lesspipe(1)
31 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
32
33 # set variable identifying the chroot you work in (used in the prompt below)
34 if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
35 debian_chroot=$(cat /etc/debian_chroot)
36 fi
37
38 # set a fancy prompt (non-color, unless we know we "want" color)
39 case "$TERM" in
40 xterm-color|*-256color) color_prompt=yes;;
41 esac
42
43 # uncomment for a colored prompt, if the terminal has the capability; turned
44 # off by default to not distract the user: the focus in a terminal window
45 # should be on the output of commands, not on the prompt
46 #force_color_prompt=yes
47
48 if [ -n "$force_color_prompt" ]; then
49 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
50 # We have color support; assume it's compliant with Ecma-48
51 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
52 # a case would tend to support setf rather than setaf.)
53 color_prompt=yes
54 else
55 color_prompt=
56 fi
57 fi
58
59 if [ "$color_prompt" = yes ]; then
60 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
61 else
62 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
63 fi
64 unset color_prompt force_color_prompt
65
66 # If this is an xterm set the title to user@host:dir
67 case "$TERM" in
68 xterm*|rxvt*)
69 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
70 ;;
71 *)
72 ;;
75 # enable color support of ls and also add handy aliases
76 if [ -x /usr/bin/dircolors ]; then
77 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
78 alias ls='ls --color=auto'
79 #alias dir='dir --color=auto'
80 #alias vdir='vdir --color=auto'
81
82 alias grep='grep --color=auto'
83 alias fgrep='fgrep --color=auto'
84 alias egrep='egrep --color=auto'
85 fi
86
87 # colored GCC warnings and errors
88 #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
89
90 # some more ls aliases
91 alias ll='ls -alF'
92 alias la='ls -A'
93 alias l='ls -CF'
94
95 # Add an "alert" alias for long running commands. Use like so:
96 # sleep 10; alert
97 alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
98
99 # Alias definitions.
100 # You may want to put all your additions into a separate file like
101 # ~/.bash_aliases, instead of adding them here directly.
102 # See /usr/share/doc/bash-doc/examples in the bash-doc package.
103
104 if [ -f ~/.bash_aliases ]; then
105 . ~/.bash_aliases
106 fi
107
108 # enable programmable completion features (you don't need to enable
109 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
110 # sources /etc/bash.bashrc).
111 if ! shopt -oq posix; then
112 if [ -f /usr/share/bash-completion/bash_completion ]; then
113 . /usr/share/bash-completion/bash_completion
114 elif [ -f /etc/bash_completion ]; then
115 . /etc/bash_completion
116 fi
117 fi
118 export WORKON_HOME=$HOME/.virtualenvs
119 source /usr/local/bin/virtualenvwrapper.sh


通过-p创建特定python版本的虚拟环境



$ mkvirtualenv -p python python2_tornado


10、安装tree,树状查看目录


$ sudo apt-get install tree


11、使用scp拷贝文件


本地文件复制到远程


$ scp aaa.txt Arvin@192.168.170.129://home/Arvin/work/scp_test

The authenticity of host '192.168.170.129 (192.168.170.129)' can't be established.
ECDSA key fingerprint is 25:ee:20:66:89:d3:82:7b:0b:61:2a:05:d8:35:5e:c3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.170.129' (ECDSA) to the list of known hosts.
Arvin@192.168.170.129's password:
aaa.txt 100% 68 0.1KB/s 00:00


本地目录复制到远程



$ scp -r aaa Arvin@192.168.170.129://home/Arvin/work/scp_test/
Arvin@192.168.170.129's password:
aaa.txt 100% 68 0.1KB/s 00:00 
bbb.txt 100% 0 0.0KB/s 00:00 
aaa.txt 100% 68 0.1KB/s 00:00


远程文件复制到本地


$ scp Arvin@192.168.170.129://home/Arvin/work/scp_test/bbb.txt ./

Arvin@192.168.170.129's password:
bbb.txt 100% 0 0.0KB/s 00:00 


远程目录下文件及目录复制到本地


$ scp -r Arvin@192.168.170.129://home/Arvin/work/scp_test/aaa/* ./
Arvin@192.168.170.129's password:
aaa.txt 100% 68 0.1KB/s 00:00 
aaa.txt 100% 68 0.1KB/s 00:00 
bbb.txt 100% 0 0.0KB/s 00:00 


远程目录复制到本地



fanyu111@fanyu:~/work/work/scp_test$ scp -r Arvin@192.168.170.129://home/Arvin/work/scp_test/aaa/ ./
Arvin@192.168.170.129's password:
aaa.txt 100% 68 0.1KB/s 00:00 
bbb.txt 100% 0 0.0KB/s 00:00 
aaa.txt 100% 68 0.1KB/s 00:00 


12、使用rz和sz拷贝文件


sz用法:



下载一个文件



sz filename


下载多个文件



sz filename1 filename2


下载dir目录下的所有文件,不包含dir下的文件夹



sz dir/*


rz用法:



输入rz回车后,会出现文件选择对话框,选择需要上传文件,一次可以指定多个文件,上传到服务器的路径为当前执行rz命令的目录。


13、修改文件权限


修改文件夹权限



$ sudo chmod -R 777 aaa


修改文件夹下文件权限



$ sudo chmod 777 aaa/*


修改文件所有者



$ chown fanyu111 aaa


修改文件所有者和组



$ chown - R fanyu111.bbb aaa


14、查看最大打开文件数


$ cat /proc/sys/fs/file-max
399181


15、安装flask


$pip install flask


16、动态查看log日志文件


$ tailf aaa.log
$ tail -f aaa.log


17、安装pycharm


18、安装mysql


$sudo apt-get install mysql-server
$sudo apt-get install mysql-client
$sudo apt-get install libmysqlclient-dev


注:安装过程中需要设置密码,不要忘了。


查看安装是否成功



$sudo netstat -apn | grep mysql



登录mysql



$mysql -u root -p


-u 表示选择登陆的用户名,



-p 表示登陆的用户密码,



上面命令输入之后会提示输入密码,此时输入密码就可以登录到mysql


查看mysql编码


mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> show variables like '%colla%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)


全局修改字符编码设置为UTF-8


默认情况下,MySQL的字符集是latin1,因此在存储中文的时候,会出现乱码的情况,所以我们需要把字符集统一改成UTF-8,用vi打开MySQL服务器的配置文件my.cnf


$sudo vi /etc/mysql/my.cnf


在[client]标签下,增加客户端的字符编码


[client]
default-character-set=utf8



在[mysqld]标签下,增加服务器端的字符编码



[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

1 #
2 # The MySQL database server configuration file.
3 #
4 # You can copy this to one of:
5 # - "/etc/mysql/my.cnf" to set global options,
6 # - "~/.my.cnf" to set user-specific options.
7 #
8 # One can use all long options that the program supports.
9 # Run program with --help to get a list of available options and with
10 # --print-defaults to see which it would actually understand and use.
11 #
12 # For explanations see
13 # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
14
15 #
16 # * IMPORTANT: Additional settings that can override those from this file!
17 # The files must end with '.cnf', otherwise they'll be ignored.
18 #
19
20 !includedir /etc/mysql/conf.d/
21 !includedir /etc/mysql/mysql.conf.d/
22
23 [client]
24 default-character-set = utf8
25
26 [mysqld]
27 character-set-server = utf8
28 collation-server = utf8_general_ci


重启mysql,生效


使用 service 启动:


sudo service mysql restart


service mysql start 启动



service mysql stop 停止


创建数据库的时候直接指定默认字符



create database *** default character set utf8


远程链接mysql


检查3306端口


$Arvin@Arvin:~$ sudo netstat -apn|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1852/mysqld


如果是绑定了本地回旋地址,需要改配置文件



$Arvin@Arvin:~$ sudo netstat -apn|grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1852/mysqld


修改mysql的配置文件文件



$Arvin@Arvin:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf



修改 bind-address = 127.0.0.1


为 bind-address = 0.0.0.0


记得要重启mysql服务



$sudo service mysql restart


创建用户



选中mysql数据库



use mysql;


查看现有用户



select user,host from user;


1、给现有用户添加远程访问权限



update user set host ='%' where user='root'; 
grant all on *.* to root@'%' identified by 'root';
flush privileges;


2、创建可以远程访问的用户


CREATE USER '<username>'@'%' IDENTIFIED BY '<password>';
GRANT ALL ON <dbname>.* TO '<username>'@'%';


3、创建只允许192.168.201.128访问的用户


CREATE USER 'user02'@'192.168.201.128' IDENTIFIED BY '<password>';
GRANT ALL ON <dbname>.* TO '<username>'@'192.168.201.128';


查看用户权限与GRANT用法



[MySQL]查看用户权限与GRANT用法


查看用户权限



show grants for 你的用户



比如:



show grants for root@'localhost';


安装PyMySQL



pip install PyMySQL



安装sqlalchemy



pip install sqlalchemy


安装memcached


$ sudo apt-get install memcached
$ pip install python-memcached


19、安装redis,在Ubuntu中执行下面这句命令:


$sudo apt-get install redis-server


启动服务端



$redis-server


启动客户端



$redis-cli 
$pip install redis


20、虚拟机与宿主机联网


宿主:Win7 , 虚拟机:Ubuntu



(1)在虚拟机中以NAT的连接模式,共享宿主机的IP地址信息



(2)在虚拟Linux系统中,ifconfig eth0 获取虚拟主机的网卡分配的IP地址如:ip。



(3)通过在宿主系统中的网址输入:ip,即可访问虚拟设备的服务。


设置PyCharm工具的编码格式:



File -> setting -> Editor -> File Encodings -> IDE Encoding 和 Project Encoding的值为:utf-8


使用pycharm链接虚拟机


——————1、链接虚拟机———————–


打开pycham,windows下连接服务器端


Tools –> Deployment –> Configurasion


点击 + 号



name 随便写



type SFTP


配置连接服务器,name随便写,connection下,协议sftp,服务器主机IP 172.16.163.10,用户名,密码


点击Test SFTP connection测试 成功显示ok


——————-2、windows下代码与服务器代码链接—————————–


下面选择连接windows下的那部分代码和服务器上代码相连,Mappings,本地Local path,服务器path,apply,OK,表示已经把本地的代码和服务器代码连接上了。


设置如何使得本地代码和服务器代码同步更新



Tools –> Deployment –> Options


在upload changed files automatically to the default server 选项中



选择On explicit save action(ctrl +s)表示在pycharm里修改代码后ctrl+s便可同步到服务器。


never 从未



always 总是


on explicit save action 显式保存动作


———————3.在pycharm里可以使用ssh——————————————————–


在pycharm里


Tools->start ssh session可以使用ssh,


如果ssh时候发现打开中文乱码,在settings里修改


File –> Tools –> SSH Terminal –> Deployment server 选 Arvin –> Default encoding: 选 UTF-8


——————–4.Windows下的Pycharm远程连接虚拟机的Python环境————————————


Windows下的Pycharm远程连接虚拟机的Python环境


打开Pycharm,File—>Settings—>Project—>Project Interpreter 选择Add Remote


选择SSH Credentials,填写参数


———————5运行virtualenv中的python————————- ——–



看python解释器位置



$which python

(python2_tornado) Arvin@Arvin:~/work/tornado_test/Arvin/1_lesson_helloworld$ which python

/home/Arvin/.virtualenvs/python2_tornado/bin/python


———————————————————————————



使用xshell链接虚拟机


使用Navicat链接虚拟机


21、安装curl


sudo apt install curl



22、清除浏览器缓存


Ctrl+Shift+Del 清除Google浏览器缓存的快捷键


Ctrl+Shift+R 重新加载当前网页而不使用缓存内容


使用locale命令查看系统当前编码



ubuntu@ubuntu:~$ locale
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_US:en
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
ubuntu@ubuntu:~$


VMWare 虚拟机备份



虚拟机->快照->拍摄快照 进行备份


豆瓣源安装



pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com flask



版权声明:本文为Arvin_zx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。