時間 | 版本 | 修改人 | 描述 |
---|---|---|---|
2023年1月10日14:04:15 | V0.1 | 宋全恒 | 新建文檔 |
2023年2月6日11:03:45 | V0.2 | 宋全恒 | 添加快捷指令別名的實(shí)現(xiàn)方便虛擬環(huán)境的切換 |
簡介
使用 PyCharm,您可以使用位于另一臺計(jì)算機(jī)(服務(wù)器)上的解釋器調(diào)試應(yīng)用程序。
參考該博客
工具菜單
?在使用PyCharm時,發(fā)現(xiàn)菜單欄中沒有工具菜單欄,通過如下的方式進(jìn)行配置。
配置過程
部署配置
這個步驟的主要目標(biāo)是可以通過PyCharm自動將本地程序的變更同步到遠(yuǎn)程的Linux服務(wù)器,并且當(dāng)本地文件和服務(wù)器的文件變更時,可以雙向獲取更新。
?在工具菜單->部署-> 配置,創(chuàng)建SFTP連接(遠(yuǎn)程文件傳輸協(xié)議)、配置映射和排除的路徑
連接配置
?配置過程如下:
?這樣連接配置就正常了,
映射配置
根路徑配置
?在連接配置時,注意根路徑對于Linux服務(wù)器來說,為“/”即可,默認(rèn)為"/root",我的項(xiàng)目打算部署在路徑/home/sqh/FastBuild目錄下,則根路徑為/即可。
映射配置
可以同時添加多個映射,在項(xiàng)目有多個子項(xiàng)目,分別部署到不同位置時。
排除的路徑
本小節(jié)主要用于一些不需要的同步,比如說pycharm產(chǎn)生的.idea目錄,或者在服務(wù)器上產(chǎn)生的日志文件,等不需要同步的內(nèi)容可以采用這種方式進(jìn)行過濾。
將項(xiàng)目部署到服務(wù)器
?操作PyCharm部署項(xiàng)目到服務(wù)器的圖示如下:
?然后使用XShell連接34服務(wù)器,查看上傳的文件目錄
配置python解釋器
?在博客中,查看了上述兩個方法進(jìn)行遠(yuǎn)程調(diào)試,作者推薦第一種。簡單瀏覽了一下第二種方式,需要下載pydevd-pycharm軟件包,并且同時需要修改源程序,比較復(fù)雜,不再演示。有問題參考博客即可。
創(chuàng)建python虛擬環(huán)境
?首先創(chuàng)建python虛擬環(huán)境,主要使用virtualenv實(shí)現(xiàn)的。
下載virtualenv軟件
?軟件安裝需要使用pip工具。
$ pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple/
創(chuàng)建虛擬環(huán)境
# cd /home/sqh/FastBuild
$ virtualenv envname # 創(chuàng)建一個名字為envname的虛擬環(huán)境
$ virtualenv -p python2 envname # 如果安裝了多個python版本,如py2和py3,需要指定使用哪個創(chuàng)建虛擬環(huán)境
# 注意,要指定python3.7以上的環(huán)境為FastBuild搭建環(huán)境
$ virtualenv -p /usr/local/dros/python/bin/python3 fastbuild
# 下面的程序用于驗(yàn)證
(fastbuild) root@szfyd-alg02:/home/sqh/FastBuild# /usr/local/dros/python/bin/python3 -V
Python 3.7.14
?具體執(zhí)行如下:
(fastbuild) root@node34-a100:/home/sqh/FastBuild# virtualenv fastbuild
created virtual environment CPython3.7.0.final.0-64 in 353ms
creator CPython3Posix(dest=/home/sqh/FastBuild/fastbuild, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
root@node34-a100:/home/sqh/FastBuild# cd fastbuild/
root@node34-a100:/home/sqh/FastBuild/fastbuild# ls
bin lib pyvenv.cfg
root@node34-a100:/home/sqh/FastBuild/fastbuild# source bin/activate
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# pip list
Package Version
---------- -------
pip 22.2.2
setuptools 63.4.1
wheel 0.37.1
[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip
?可以看到在/home/sqh/FastBuild創(chuàng)建了虛擬環(huán)境fastbuild:
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# ll
total 24
drwxr-xr-x 4 root root 4096 Jan 11 02:52 ./
drwxr-xr-x 10 root root 4096 Jan 11 02:52 ../
drwxr-xr-x 2 root root 4096 Jan 11 02:52 bin/
-rw-r--r-- 1 root root 40 Jan 11 02:52 .gitignore
drwxr-xr-x 3 root root 4096 Jan 11 02:52 lib/
-rw-r--r-- 1 root root 245 Jan 11 02:52 pyvenv.cfg
激活虛擬環(huán)境
root@node34-a100:/home/sqh/FastBuild/fastbuild# cd /home/sqh/FastBuild/fastbuild
root@node34-a100:/home/sqh/FastBuild/fastbuild# source bin/activate
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# ll
虛擬環(huán)境操作
# 在虛擬環(huán)境下查看當(dāng)前python版本
python -V
# 在虛擬環(huán)境下查看當(dāng)前pip版本
pip -V
# 查看當(dāng)前虛擬環(huán)境安裝的模塊
pip list
# 于虛擬環(huán)境下安裝模塊
pip install module_name
取消虛擬環(huán)境
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# which python
/home/sqh/FastBuild/fastbuild/bin/python
(fastbuild) root@node34-a100:/home/sqh/FastBuild/fastbuild# pip -V
pip 22.2.2 from /home/sqh/FastBuild/fastbuild/lib/python3.7/site-packages/pip (python 3.7)
(fastbuild) root@node34-a100:/home/sqh/FastBuild# deactivate
root@node34-a100:/home/sqh/FastBuild#
root@node34-a100:/home/sqh/FastBuild# which python
/root/anaconda3/bin/python
root@node34-a100:/home/sqh/FastBuild# which pip
/root/anaconda3/bin/pip
在虛擬環(huán)境中配置FastBuild依賴
?項(xiàng)目的依賴均位于requirements.txt.
?可以看到起初創(chuàng)建的虛擬環(huán)境中沒有什么軟件,執(zhí)行軟件安裝,下載需要的依賴。
確保已經(jīng)激活了fastbuild環(huán)境。
(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip list
Package Version
---------- -------
pip 22.2.2
setuptools 63.4.1
wheel 0.37.1
[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip
(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip install -r requirements.txt
Collecting anyio==3.6.1
Using cached anyio-3.6.1-py3-none-any.whl (80 kB)
Collecting APScheduler==3.9.1
Using cached APScheduler-3.9.1-py2.py3-none-any.whl (59 kB)
Collecting autopep8==1.7.0
Using cached autopep8-1.7.0-py2.py3-none-any.whl (45 kB)
Collecting bcrypt==4.0.0
Using cached bcrypt-4.0.0-cp36-abi3-manylinux_2_24_x86_64.whl (594 kB)
Collecting certifi==2022.6.15
Using cached certifi-2022.6.15-py3-none-any.whl (160 kB)
Collecting cffi==1.15.1
Using cached cffi-1.15.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (427 kB)
Collecting charset-normalizer==2.1.0
Using cached charset_normalizer-2.1.0-py3-none-any.whl (39 kB)
Collecting click==8.1.3
Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting colorama==0.4.5
Using cached colorama-0.4.5-py2.py3-none-any.whl (16 kB)
Collecting cryptography==38.0.1
Using cached cryptography-38.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (4.0 MB)
Collecting distlib==0.3.5
Using cached distlib-0.3.5-py2.py3-none-any.whl (466 kB)
Collecting fastapi==0.85.0
Using cached fastapi-0.85.0-py3-none-any.whl (55 kB)
Collecting filelock==3.8.0
Using cached filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting greenlet==1.1.3
Using cached greenlet-1.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (150 kB)
Collecting h11==0.14.0
Using cached h11-0.14.0-py3-none-any.whl (58 kB)
Collecting httptools==0.5.0
Downloading httptools-0.5.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (404 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 404.7/404.7 kB 1.7 MB/s eta 0:00:00
Collecting idna==3.3
Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting loguru==0.6.0
Using cached loguru-0.6.0-py3-none-any.whl (58 kB)
Collecting paramiko==2.11.0
Using cached paramiko-2.11.0-py2.py3-none-any.whl (212 kB)
Collecting platformdirs==2.5.2
Using cached platformdirs-2.5.2-py3-none-any.whl (14 kB)
Collecting pycodestyle==2.9.1
Using cached pycodestyle-2.9.1-py2.py3-none-any.whl (41 kB)
Collecting pycparser==2.21
Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)
Collecting pydantic==1.10.2
Using cached pydantic-1.10.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.8 MB)
Collecting PyNaCl==1.5.0
Using cached PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)
Collecting python-dotenv==0.21.0
Downloading python_dotenv-0.21.0-py3-none-any.whl (18 kB)
Collecting pytz==2022.4
Using cached pytz-2022.4-py2.py3-none-any.whl (500 kB)
Collecting pytz-deprecation-shim==0.1.0.post0
Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting PyYAML==6.0
Using cached PyYAML-6.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (596 kB)
Collecting requests==2.28.1
Using cached requests-2.28.1-py3-none-any.whl (62 kB)
Collecting six==1.16.0
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting sniffio==1.3.0
Using cached sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting SQLAlchemy==1.4.41
Using cached SQLAlchemy-1.4.41-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)
Collecting starlette==0.20.4
Using cached starlette-0.20.4-py3-none-any.whl (63 kB)
Collecting toml==0.10.2
Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting typing_extensions==4.3.0
Using cached typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting tzdata==2022.4
Using cached tzdata-2022.4-py2.py3-none-any.whl (336 kB)
Collecting tzlocal==4.2
Using cached tzlocal-4.2-py3-none-any.whl (19 kB)
Collecting urllib3==1.26.11
Using cached urllib3-1.26.11-py2.py3-none-any.whl (139 kB)
Collecting utils==1.0.1
Downloading utils-1.0.1-py2.py3-none-any.whl (21 kB)
Collecting uvicorn==0.18.3
Using cached uvicorn-0.18.3-py3-none-any.whl (57 kB)
Collecting virtualenv==20.16.3
Using cached virtualenv-20.16.3-py2.py3-none-any.whl (8.8 MB)
Collecting watchfiles==0.18.1
Downloading watchfiles-0.18.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 9.0 MB/s eta 0:00:00
Collecting websockets==10.4
Downloading websockets-10.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (106 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 106.7/106.7 kB 31.8 MB/s eta 0:00:00
Collecting win32-setctime==1.1.0
Using cached win32_setctime-1.1.0-py3-none-any.whl (3.6 kB)
Requirement already satisfied: setuptools>=0.7 in ./fastbuild/lib/python3.7/site-packages (from APScheduler==3.9.1->-r requirements.txt (line 2)) (63.4.1)
Collecting importlib-metadata
Downloading importlib_metadata-6.0.0-py3-none-any.whl (21 kB)
Collecting backports.zoneinfo
Using cached backports.zoneinfo-0.2.1-cp37-cp37m-manylinux1_x86_64.whl (70 kB)
Collecting zipp>=0.5
Downloading zipp-3.11.0-py3-none-any.whl (6.6 kB)
WARNING: The candidate selected for download or install is a yanked version: 'apscheduler' candidate (version 3.9.1 at https://files.pythonhosted.org/packages/e4/9f/c3937d4babe62504b874d4bf2c0d85aa69c7f59fa84cf6050f3b9dc5d83e/APScheduler-3.9.1-py2.py3-none-any.whl (from https://pypi.org/simple/apscheduler/) (requires-python:>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4))
Reason for being yanked: Not compatible with Python 2.7
Installing collected packages: pytz, distlib, zipp, win32-setctime, websockets, utils, urllib3, tzdata, typing_extensions, toml, sniffio, six, PyYAML, python-dotenv, pycparser, pycodestyle, platformdirs, loguru, idna, httptools, greenlet, filelock, colorama, charset-normalizer, certifi, bcrypt, backports.zoneinfo, requests, pytz-deprecation-shim, pydantic, importlib-metadata, h11, cffi, autopep8, anyio, watchfiles, virtualenv, tzlocal, starlette, SQLAlchemy, PyNaCl, cryptography, click, uvicorn, paramiko, fastapi, APScheduler
Successfully installed APScheduler-3.9.1 PyNaCl-1.5.0 PyYAML-6.0 SQLAlchemy-1.4.41 anyio-3.6.1 autopep8-1.7.0 backports.zoneinfo-0.2.1 bcrypt-4.0.0 certifi-2022.6.15 cffi-1.15.1 charset-normalizer-2.1.0 click-8.1.3 colorama-0.4.5 cryptography-38.0.1 distlib-0.3.5 fastapi-0.85.0 filelock-3.8.0 greenlet-1.1.3 h11-0.14.0 httptools-0.5.0 idna-3.3 importlib-metadata-6.0.0 loguru-0.6.0 paramiko-2.11.0 platformdirs-2.5.2 pycodestyle-2.9.1 pycparser-2.21 pydantic-1.10.2 python-dotenv-0.21.0 pytz-2022.4 pytz-deprecation-shim-0.1.0.post0 requests-2.28.1 six-1.16.0 sniffio-1.3.0 starlette-0.20.4 toml-0.10.2 typing_extensions-4.3.0 tzdata-2022.4 tzlocal-4.2 urllib3-1.26.11 utils-1.0.1 uvicorn-0.18.3 virtualenv-20.16.3 watchfiles-0.18.1 websockets-10.4 win32-setctime-1.1.0 zipp-3.11.0
[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip
(fastbuild) root@node34-a100:/home/sqh/FastBuild# pip list
Package Version
--------------------- -----------
anyio 3.6.1
APScheduler 3.9.1
autopep8 1.7.0
backports.zoneinfo 0.2.1
bcrypt 4.0.0
certifi 2022.6.15
cffi 1.15.1
charset-normalizer 2.1.0
click 8.1.3
colorama 0.4.5
cryptography 38.0.1
distlib 0.3.5
...
配置shell快捷命令
?由于在調(diào)試時,需要頻繁的使用fastbuild,切換到目錄,并激活虛擬環(huán)境,因此可以使用別名的方式引入shell命令,比如說我們引入fb命令,來自動的切換到工作目錄,并激活虛擬環(huán)境。可以使用如下配置:
?修改配置文件~/.bashrc
,添加alias語句命令,如下所示:
80 # some more ls aliases
81 alias ll='ls -alF'
82 alias la='ls -A'
83 alias l='ls -CF'
84 alias fb='cd /home/sqh/FastBuild; source fastbuild/bin/activate'
?然后
source ~/.bashrc
?可以看到,在連接服務(wù)器之后,可以直接鍵入命令fb
實(shí)現(xiàn)目錄切換和虛擬環(huán)境的激活。
*** System restart required ***
Last login: Mon Feb 6 02:13:29 2023 from 10.11.12.108
root@node34-a100:~# fb
(fastbuild) root@node34-a100:/home/sqh/FastBuild#
配置遠(yuǎn)程python解釋器
注: 主要是配置項(xiàng)目解釋器(通過SSH選取服務(wù)端的python解釋器)
?點(diǎn)擊下一步,選擇Python解釋器
注: 勾選同步文件夾,由于服務(wù)器在FastBuild目錄下產(chǎn)生了目錄fastbuild虛擬環(huán)境,因此可以使用之前排除的路徑,把該目錄排除,以防止服務(wù)器該目錄同步到本機(jī)。
相當(dāng)于在PyCharm中新建了一個python解釋器變量。接下來要指定項(xiàng)目使用該解釋器
配置使用Python解釋器
?修改項(xiàng)目調(diào)試的python解釋器環(huán)境
遠(yuǎn)程調(diào)試
?遠(yuǎn)程調(diào)試服務(wù)器代碼,就可以通過pycharm來啟動項(xiàng)目了。
文章來源:http://www.zghlxwxcb.cn/news/detail-725417.html
總結(jié)
?本文詳細(xì)的描述了使用PyCharm來調(diào)試遠(yuǎn)程Linux服務(wù)器的代碼,通過直接調(diào)試服務(wù)器上的的代碼,可以解決在Windows上的差異(比如說路徑分隔符),而且在使用FastBuild進(jìn)行鏡像構(gòu)建的時候,需要使用docker運(yùn)行環(huán)境,在真實(shí)環(huán)境上運(yùn)行能夠更加方便快捷的開發(fā)。文章來源地址http://www.zghlxwxcb.cn/news/detail-725417.html
到了這里,關(guān)于01-10 周二 PyCharm遠(yuǎn)程Linux服務(wù)器配置進(jìn)行端點(diǎn)調(diào)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!