安志林 發表於 2019-10-11 11:16:00

服务器(Linux)上运行python总结

<p>跑实验换了几次服务器了,每次遇到相似问题都要重新百度,而且每次百度搜索出的顺序都不一样,又得重新找半天,这次把遇到的问题都总结一下。</p>
<h3>1.准备</h3>
<p>PuTTY和FileZilla</p>
<p>FileZilla使用FTP传输时,实验室的服务器可能是出于安全考虑,没有开放21端口;想到ssh可以登录,就换SFTP来传输文件(22端口)。</p>
<p>也可以用scp rz sz等等,但我觉得在windows上使用FileZilla的图形界面拖拽就很方便。</p>
<h3>2.安装</h3>
<h4>2.1 Linux软件安装</h4>
<p>首先了解下Linux下软件的安装,参考Linux下软件安装的几种方式</p>
<h4>2.2 Anaconda</h4>
<p>实验室服务器之前已经装了各种版本的python,大家都是各用各的,在自己的目录下使用anaconda,我也不敢随便改/etc/profile(也没权限= =),就也在自己目录下装个anaconda吧。。</p>
<p>安装python我还是第一次用anaconda(之前一直都是pip管理),</p>
<p>参考的&nbsp;ubuntu16.04下安装&amp;配置anaconda+tensorflow新手教程,看着理解了下anaconda的安装过程,</p>
<p>配置环境变量的配置我只改了我这个用户的,也就是~/.profile,注意把 ~/anaconda/bin放到$PATH前,因为bash中执行python命令时会按PATH中的顺序进行搜索。</p>
<p>我的配置:</p>
<div class="cnblogs_code">
<pre>#umask <span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">

# </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> running bash
</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ -n <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$BASH_VERSION</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    # include .bashrc </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> it exists
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> [ -f <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/.bashrc</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
      . </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/.bashrc</span><span style="color: rgba(128, 0, 0, 1)">"</span>
    <span style="color: rgba(0, 0, 255, 1)">fi</span>
<span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">

# set PATH so it includes user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s private bin if it exists</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> [ -d <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/bin</span><span style="color: rgba(128, 0, 0, 1)">"</span> ] ; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/bin:$PATH</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">

# ADD_PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$(pwd)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
export PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/anaconda3/bin:$PATH</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
# export PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${ADD_PATH}:${PATH}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
export GUROBI_HOME</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/tars/gurobi811/linux64</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
export PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${PATH}:${GUROBI_HOME}/bin</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
export LD_LIBRARY_PATH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
export GRB_LICENCE_FILE</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$HOME/gurobi.lic</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>安装完后自己理解了下anaconda对python packages的管理</p>
<div class="cnblogs_code">
<pre>(base) tandaojun1@node32:~<span style="color: rgba(0, 0, 0, 1)">$ python3
Python </span><span style="color: rgba(128, 0, 128, 1)">3.7</span>.<span style="color: rgba(128, 0, 128, 1)">3</span> (default, Mar <span style="color: rgba(128, 0, 128, 1)">27</span> <span style="color: rgba(128, 0, 128, 1)">2019</span>, <span style="color: rgba(128, 0, 128, 1)">22</span>:<span style="color: rgba(128, 0, 128, 1)">11</span>:<span style="color: rgba(128, 0, 128, 1)">17</span><span style="color: rgba(0, 0, 0, 1)">)
:: Anaconda, Inc. on linux
Type </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">help</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">copyright</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">credits</span><span style="color: rgba(128, 0, 0, 1)">"</span> or <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">license</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">for</span> <span style="color: rgba(0, 0, 255, 1)">more</span><span style="color: rgba(0, 0, 0, 1)"> information.
</span>&gt;&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> import sys
</span>&gt;&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> print(sys.path)
[</span><span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/home/tandaojun1/anaconda3/lib/python37.zip</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/home/tandaojun1/anaconda3/lib/python3.7</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/home/tandaojun1/anaconda3/lib/python3.7/lib-dynload</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/home/tandaojun1/anaconda3/lib/python3.7/site-packages</span><span style="color: rgba(128, 0, 0, 1)">'</span>]</pre>
</div>
<p>所以,之后用conda install tensorflow下载的第三方包应该是会装到/home/tandaojun1/anaconda3/lib/python3.7/site-packages目录下</p>
<p>bash前面的(base)是conda的虚拟环境,conda activate base进入的是默认虚拟环境,base换成自己的环境,前面就当前环境的环境名。</p>
<p>另外,conda的虚拟环境执行bash确实有点延迟,,</p>
<h4>2.3 Gurobi</h4>
<p>这个是我的程序需要的一个求解器,第三方包,conda没有,需要自己安装</p>
<p>首先求解器的安装是从官网wget下载tar.gz文件,然后tar -vxfz gurobi811.tar.gz解压,目前只是得到了求解器</p>
<p>在~/.profile中添加gurobi相关的环境变量(PATH和LD_LIBRARY_PATH),然后grbgetkey获取licence,再在~/.profile中添加GRB_LICENCE_FILE环境变量</p>
<p>目前求解器可以求解了,但python还没有安装packages,进入到~/gurobi811/linux64/路径下</p>
<p>python setup.py install</p>
<p>可以自己看下setup.py代码是怎么写的,以后也会用到。</p>
<h3>3.安装测试</h3>
<p>其实上面已经测试了= =,不过可以再记下确认用的是哪个版本(哪个路径下的sh)的python (也可以用type python)</p>
<div class="cnblogs_code">
<pre>(base) tandaojun1@node32:~$ <span style="color: rgba(0, 0, 255, 1)">which</span><span style="color: rgba(0, 0, 0, 1)"> python
</span>/home/tandaojun1/anaconda3/bin/python</pre>
</div>
<p>要查看系统中有哪些python:</p>
<div class="cnblogs_code">
<pre>(base) tandaojun1@node32:~$ <span style="color: rgba(0, 0, 255, 1)">whereis</span><span style="color: rgba(0, 0, 0, 1)"> python
python: </span>/usr/bin/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /usr/bin/python3.4m-config /usr/bin/python3.<span style="color: rgba(128, 0, 128, 1)">4</span>-config /usr/bin/python3.4m /usr/bin/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /usr/bin/python2.<span style="color: rgba(128, 0, 128, 1)">7</span>-config /usr/bin/python /etc/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /etc/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /etc/python /usr/lib/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /usr/lib/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /usr/bin/X11/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /usr/bin/X11/python3.4m-config /usr/bin/X11/python3.<span style="color: rgba(128, 0, 128, 1)">4</span>-config /usr/bin/X11/python3.4m /usr/bin/X11/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /usr/bin/X11/python2.<span style="color: rgba(128, 0, 128, 1)">7</span>-config /usr/bin/X11/python /usr/local/lib/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /usr/local/lib/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /usr/include/python3.<span style="color: rgba(128, 0, 128, 1)">4</span> /usr/include/python3.4m /usr/include/python2.<span style="color: rgba(128, 0, 128, 1)">7</span> /usr/share/python /usr/share/<span style="color: rgba(0, 0, 255, 1)">man</span>/man1/python.<span style="color: rgba(128, 0, 128, 1)">1</span>.gz</pre>
</div>
<p>&nbsp;</p>
<h3>4.运行</h3>
<h4>4.1 命令行python脚本传参</h4>
<p>首先了解下命令行中怎么在执行python文件时传入参数,可以参考&nbsp;&nbsp;命令行运行Python脚本时传入参数的三种方式</p>
<h4>4.2 python import model</h4>
<p>其次是理解下python的import机制,因为windows下pycharm会帮你把sys.path(上面演示的)等给你预置好了,直接执行就可以,但在命令行中执行会遇到ModuleNotFoundError。</p>
<p>我看的关于Python的import机制原理,主要是知道python从哪import models。</p>
<p>明白了这个,接下来就是怎么让python找到自己写的models。</p>
<p>一种方法是在.py文件中使用sys.path.append()来添加,可以参考https://blog.csdn.net/qq_43355223/article/details/87340858和https://www.cnblogs.com/bob-jianfeng/p/10274034.html</p>
<p>使用绝对路径显然不适用于经常换平台执行,但使用相对路径得确保要执行的文件与项目根目录之间的相对关系不变,我这代码还在测试阶段,可能会经常变动= =,所以想在命令行直接将项目根目录添加到python找models的列表中。</p>
<p>一开始以为是PATH变量,尝试将项目目录添加到PATH变量中,发现还是会报错,之后百度了才知道,,,是PYTHONPATH</p>
<p>可以在~/.profile中添加(记得 source ~/.profile),也可以命令行执行(每次重新开启终端都要执行)</p>
<p>export PYTHONPATH=~/projects/vnf_flow:$PYTHONPATH</p>
<h4>4.3 后台运行</h4>
<p>由于我要跑多个实验,每个实验都要输出一些结果,所以需要进程后台运行,</p>
<p>screen就很不错,可以参考Linux后台运行任务</p><br><br>
来源:https://www.cnblogs.com/peanutk/p/11652993.html
頁: [1]
查看完整版本: 服务器(Linux)上运行python总结