python相关【git、pip、jupyter、虚拟环境等杂记】

git

git新建项目

git init
git remote add origin xxxxxxxxxxxxxxxxxxxxx.git
git add .
git commit -m "*****"
git push

如果空项目里创建了文件,那么需要先拉取仓库,这时需要允许历史不一致

git pull origin master --allow-unrelated-histories

git 下载太慢

从这里下:
https://npm.taobao.org/mirrors/git-for-windows/

git 撤销add

git reset HEAD

git 撤销commit

git reset --soft HEAD^

git 删除本地文件

git rm -r xxx

git 删除本地仓库文件

git rm -r --cached xxx

git 删除远程仓库文件

git rm -r --cached xxx
git commit -m “delete xxx”
git push

git 修改远程仓库地址

git remote set-url origin xxxxx.git

git 查看修改用户信息

git config --local --list
git config user.name
git config user.email

git config --global user.name xxxxx
git config --global user.email xxxxx@xxxx

安装lfs

sudo apt-get install git-lfs

用python查看git安装的包

from pip._internal.utils.misc import get_installed_distributions
piplist = get_installed_distributions
for x in piplist:print(x)


pip

安装cv2

pip install opencv-python

查看配置

pip config list

ReadTimeoutError

pip 安装的时候超时,可能是因为源的问题,也可能是因为阈值设置太低了

Windows下换源:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

修改时间限制:

pip --default-timeout=1000 install -U pip

配置默认源

pip config set global.index-url https://pypi.douban.com/simple/

http://mirrors.aliyun.com/pypi/simple/

http://pypi.douban.com/simple/

https://pypi.tuna.tsinghua.edu.cn/simple/

http://pypi.mirrors.ustc.edu.cn/simple/

阿里云 http://mirrors.aliyun.com/pypi/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/


linux

系统信息

查看显卡

lspci | grep -i vga

查看现存

nvidia-smi

检查cuda

nvcc --version

cat /usr/local/cuda/version.txt

检车cudnn

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

scp

scp -P [端口号] -r [文件夹名] [用户名]@[IP地址]:[目标目录]

scp -P [端口号] [文件名] [用户名]@[IP地址]:[目标目录]

apt-get

如果提示如下,表明被占用

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

解决方法:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a
sudo apt update

防火墙

查看状态

systemctl status firewalld.service

关闭

systemctl stop firewalld.service

systemctl disable firewalld.service

开启

systemctl start firewalld.service

systemctl enable firewalld.service

虚拟机共享文件夹

在Windows中设置好共享文件夹,并在vm中设置对应文件夹路径

在虚拟机里的Ubuntu中安装dkms

sudo apt-get install open-vm-tools-dkms
sudo apt-get install open-vm-dkm

另一种成功:

在电脑中配置文件夹,设置为共享,并在vm中设置对应文件夹路径

mkdir /mnt/hgfs

#将所有共享文件夹挂载到/mnt/hgfs:
sudo vmhgfs-fuse .host:/ /mnt/hgfs -o subtype=vmhgfs-fuse,allow_other

#将名称sharedfolder的共享挂载到/mnt/hgfs
sudo vmhgfs-fuse .host:/sharedfolder /mnt/hgfs -o subtype=vmhgfs-fuse,allow_other

如果提示如下,表明被占用

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

解决方法:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a
sudo apt update

wegt太慢

wget http://jaist.dl.sourceforge.net/project/kmphpfm/mwget/0.1/mwget_0.1.0.orig.tar.bz2
yum install bzip2 gcc-c++ openssl-devel intltool -y
bzip2 -d mwget_0.1.0.orig.tar.bz2
tar -xvf mwget_0.1.0.orig.tar
cd mwget_0.1.0.orig
./configure
make
make install

编译gcc

下载gcc:

wget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz

mwget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz

wget https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz

mwget https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz

解压:

tar xf gcc-7.5.0.tar.gz

cd gcc-7.5.0/

安装环境:

./contrib/download_prerequisites

创建临时文件夹编译:

cd …

mkdir gcc-7.5-build

cd gcc-7.5-build

…/gcc-7.5.0/configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

make

make instal

docker

删除所有容器

docker rm `docker ps -a -q`

删除所有镜像

docker rmi `docker images -q`

删除所有没有tag的镜像

docker rmi `docker images|grep none|awk '{print $3 }'|xargs`

删除指定的镜像

docker rmi --force `docker images |grep pt-* |awk '/0108/ {print $3}'`
docker rmi -f `docker images | grep pt-*| awk '{print $3}'` 

vi/vim

vi的三种模式

  • 一般命令模式(command mode):

    光标的移动,搜索,替换,删除字符,删除整行,复制整行,粘贴整行。

  • 编辑模式(insert mode):

    插入或替换编辑文件内容。

    底下会显示 INSERTREPLACE 字样。

  • 命令行模式(command-line mode):

    读取、保存、批量替换等其他额外功能。

三种模式的切换

  • 其他模式 -> 一般命令模式:ESC
  • 一般命令模式 -> 编辑模式:i/Io/Oa/A(插入)r/R(替换)
  • 一般命令模式 -> 命令行模式::/?

常用命令

1.光标

移动一个字符:方向键上下左右ijhk

上下翻页:Ctrl + f(向下)+ b(向上)

行首:0Home

行尾:$End

文件首行:gg

文件末行:G

向下n行:n <Enter>

2.查找替换

向下查找word:/word,(?word 向上)

替换n1与n2行之间的 word1 为 word2: :n1,n2s/word1/word2/g

替换全文的 word1 为 word2::1,$s/word1/word2/g:%s/word1/word2/g1

最后的/g如果改成/gc表示替换前需确认

3.删除、复制、粘贴

删除:x:相当于del、X:相当于backspace

剪切整行:dd

复制整行:yy

粘贴:p:粘贴到光标下一行,P:粘贴到光标上一行

撤销:u

重做:ctrl + r

注意:

ndd,nyy:相当于n次dd,n次yy

d,y后面加1G,G,$,0:分别表示从光标处一直到首行,末行,行尾,行首

例如d1G表示删除当前行到首行,y0表示复制当前字符到行尾

4.进入编辑模式

i :从目前光标所在处输入

I :在目前所在行的第一个非空格符处开始输入

a :从目前光标所在的下一个字符处开始输入』

A :从光标所在行的最后一个字符处开始输入』

o :在目前光标所在的下一行处输入新的一行』

O :在目前光标所在处的上一行输入新的一行

r :只会取代光标所在的那一个字符一次

R:会一直取代光标所在的文字,直到按下 ESC 为止

5.保存、退出

:w :保存

:w! :强制保存

:q :关闭

:q! :强制关闭(不保存)

:wq :保存关闭

:wq! :强制保存关闭

:w [filename] :另存为

:set nu :显示行号

:set nonu :取消显示行号


虚拟环境

环境创建切换

使用conda

创建环境:

conda create -n [虚拟环境名称,例如env] pip python=[想要装的python版本,例如3.7]

查看环境:

conda info --envs

激活环境:

conda activate [虚拟环境名称]

退出环境:

conda deactivate [虚拟环境名称]

使用virtualenv

创建环境:

virtualenv [虚拟环境名称,例如env]

virtualenv -p [之前配置过的环境路径]/python3 [虚拟环境名称]

查看环境:

workon
lsvirtualenv

激活环境:

workon [虚拟环境名称]

退出环境:

deactivate

删除环境:

rmvirtualenv [虚拟环境名称]

环境配置

导出requirements:

pip freeze > requirements.txt

导入requirements:

pip install -r requirements.txt


其他

pycharm 文件初始化模板

在这里插入图片描述

常用变量:

$ {PROJECT_NAME} - 当前项目的名称。

$ {NAME} - 在文件创建过程中在“新建文件”对话框中指定的新文件的名称。

$ {USER} - 当前用户的登录名。

$ {DATE} - 当前的系统日期。

$ {TIME} - 当前系统时间。

$ {YEAR} - 今年。

$ {MONTH} - 当月。

$ {DAY} - 当月的当天。

$ {HOUR} - 目前的小时。

$ {MINUTE} - 当前分钟。

$ {PRODUCT_NAME} - 将在其中创建文件的IDE的名称。

$ {MONTH_NAME_SHORT} - 月份名称的前3个字母。 示例:1月,2月等

$ {MONTH_NAME_FULL} - 一个月的全名。 示例:1月,2月等

常用模板:

# -*- coding: utf-8 -*-
# @Time    : ${DATE} ${TIME}
# @Author  : xxxxxxxxx
# @Email   : xxxxxxxxxxx@xxx.com
# @File    : ${NAME}.py
# @Software: ${PRODUCT_NAME}

jupyter

清除运行结果

Cell -> All Output -> Clear

在这里插入图片描述

jupyter魔法命令

%lsmagic

out:

Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode

Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

.ipyub 转成其他格式文件

应该是可以直接转成:asciidoc, custom, html, latex, markdown, notebook, pdf, python, rst, script, slides 这些格式的。
如果需要放到 .md 文件里面可以转成html,然后再用插入html代码的方法显示

jupyter nbconvert --to html xxxx.ipynb

但是很小一个.ipyub 转成后的html都很大甚至上万行,还是别轻易放到md里了

.proto 转 .py

参考 object_detection 的:TensorFlow 08——ch05-TensorFlow Object Detection 深度学习目标检测

在对应目录直接用protoc就行:

protoc object_detection/protos/*.proto --python_out=.

ConfigParser()

ConfigeParser类的作用是对配置文件进行相关的操作。

config.ini 文件的格式例如:

[#section_name1]
#key1A = #value1A
#key1B = #value1B

操作有:

import  configparser
 
config = configparser.ConfigParser()
file = 'config.ini'
config.read(file)

# 读取配置的两种方式
A = config['#section_name1']['#key1A']
B = config.get('#section_name1','#key1B')

# 判断有没有#section_name1这个section
s1 = config.has_section('#section_name1')

# 判断#section_name1有没有#key1A这个key
kA = config.has_option('#section_name1','#key1A')

# 读取键值#section_name1的所有key
keys1 = config.options('#section_name1')

# 修改配置
config.set('#section_name1','#key1A','#value1a')
config.set('#section_name1','#key1B','#value1b')

# 添加配置
config.add_section('#section_name2')
config.set('#section_name2','#key2A','#value2A')
config.set('#section_name2','#key2B','#value2B')

# 删除配置
config.remove_option('#section_name2','#key2A','#value2A')
config.remove_option('#section_name2','#key2B','#value2B')
config.remove_section('#section_name2')

with open(file,'w') as configfile:
    config.write(configfile)

注意: 修改ini文件后要用‘w’打开文件重新write才会生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值