程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python environment configuration

編輯:Python

One .python Environment configuration and virtual environment construction

1. Download from the official website python Install after installing package

2.python Environment variable configuration windows

C:\Users\Administrator\AppData\Local\Programs\Python\Python36C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts

3.pip

(1).pip Source modification

  1. Temporary use :
    In the use of pip When adding parameters -i
 for example :pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gevent

frequently-used pip The sources are as follows :

pypi Tsinghua University source :https://pypi.tuna.tsinghua.edu.cn/simplepypi Douban source :http://pypi.douban.com/simple/pypi Tencent source :http://mirrors.cloud.tencent.com/pypi/simplepypi Aliyuan :https://mirrors.aliyun.com/pypi/
  1. Permanent modification : linux Next :
mkdir ~/.pipvi ~/.pip/pip.conf Add the following : [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple

windows Next , Directly in user Create a pip Catalog , Such as :C:\Users\xx\pip, New file pip.ini, The contents are as follows

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

(2).pip Use

<1>.pip freeze

The code is as follows ( Example ):

pip freeze > requirements.txt # The version number of the dependent package may be lost # perhaps pip list --format=freeze> requirements.txt

Generate requirements.txt,pip freeze Will the current PC All installation packages in the environment are generated , Many packages will be installed when installing again . It's not advisable to spend time and effort .

<2>.pipreqs

Use pipreqs, The advantage of this tool is that it can scan the project directory , Discover which libraries are used , Generate dependency list

pip install pipreqs

stay python The root directory of the project Use pipreqs ./

pipreqs ./ --encoding=utf8INFO: Successfully saved requirements file in ./requirements.txt

: Reappear

 The final product is requirements.txt, You can download all the dependencies from this file .pip install -r requirements.txt# Temporary source change pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

4. Virtual environment creation

(1 ) Windows Install separately under python

1. install virtualenv

pip install virtualenvpip install virtualenvwrapper # That's right virtualenv The packaged version of , Must be in virtualenv After the installation 

2. Creating a virtual environment

 Select a file to store the virtual environment , Such as E:/python3cd /d E:python3 # Enter the file virtualenv cui_py37 # Create a name for envname Virtual environment for dir # Check the current directory to know a envname The file of has been created 

3. Start the virtual environment

cd envname/Scripts # Go to the relevant startup folder activate # Start the virtual environment deactivate # Exit virtual environment 

Now you are free to install various packages in the virtual environment

(2) ubuntu Environment virtual environment construction

1 Creating a virtual environment

virtualenv venv_cui -p python3.7

2 Activate the virtual environment

source venv_cui/bin/active

3 Exit virtual environment

deactivate

(3) poetry Environmental management https://code.infervision.com/starship/starship_universal_dev_env

1. be based on venv install virtualenv

sudo apt-get update && apt-get install -y virtualenv

2. Create a virtual environment

virtualenv -p python3.7 venv

3. Activate the virtual environment

source venv/bin/activate

4. Install in this virtual environment poetry

python3.7 -m pip install poetry==1.1.11
  1. take poetry.lock and pyproject.toml Copy the file to the created virtual environment directory

  2. Direct will poetry Install the package of into the current virtual environment

poetry install

Two .anaconda Configure environment variables

1. Environment variable configuration

Put the following 3 Add a path folder to the environment variable ( Modify according to the actual installation path of your computer )

C:\Users\xiao_CUI\Anaconda3C:\Users\xiao_CUI\Anaconda3\Library\binC:\Users\xiao_CUI\Anaconda3\Scripts

2. Creating a virtual environment

installed Anaconda python 3.7, Want to use python3.6 Method
(1)cmd Use command :

 conda create -n py36_cui python=3.6 

(2) After installed , There will be hints. :

conda activate py36 # Activate the environment conda deactivate # Out of the environment 

3.juypter Default folder settings

Be careful not to have Chinese and spaces in the path , Otherwise, the set path will not take effect
Open directly by default jupyternotebook Will open User's personal directory , That is to say c:/users/** If you want to change your project directory

3.1 Generate configuration files

First you need to generate the configuration file , stay cmd Input from the command line jupyter notebook --generate-config

3.2 Modify the configuration file

If the configuration file just generated ok 了 , Then go to find the file in this path
Open the file and find the following line

 Get rid of # Number , And put your project path in quotation marks c.NotebookApp.notebook_dir = 'F:/cuidemo' 

Such as my
If your folder appears \n In this way Need to escape again , It's written in \n Save after changing

3.3 Get rid of %***%

application , Just ok 了 , Restart jupyternotebook

4.anaconda command

1. install modular

 conda install XXXX

2. Check out the installation package

 conda list

3. Uninstall package

conda remove XXXX

4. Upgrade package

 conda update
  1. anaconda Virtual environment installation
    Installing a virtual environment 【TensoFlow2 install 】
    open Anaconda Prompt
conda create -n TF2.1 python==3.7conda activate TF2.1 conda install cudatoolkit=10.1 conda install cudnn=7.6 pip install tensorflow==2.1 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade tensorflow==2.1

Test for successful installation

import tensorflow as tfprint(tf.__version__) 

6…anaconda Download link

1.anaconda Historical version download link
https://repo.continuum.io/archive/

  1. anaconda download original site :https://www.anaconda.com/download/ ( Download is slow )
    Tsinghua University image :https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ ( It is recommended to download Tsinghua mirror version , The download will be fast

7.anaconda Common problems and solutions

7.1 open jupyter notebook Flash back , Will not open the browser , Don't complain
terms of settlement :
1. stay cmd Input jupyter notebook View error message , Then Baidu
2.tornado Version too high and jupyter notebook Mismatch , Install a lower version of tornado 【pip install tornado==4.5.3】

3、 ... and .git To configure

1. Install locally first git

2. Configure the username and mailbox

git config --global user.name "jeremy" (github The user name registered on the official website )git config --global user.email "[email protected]" (gitub Register the bound email on the official website )

Check the configuration :git config --list

3 To configure SSH

 Not configured SSH,[email protected] Of URL Can't download , Large files are transferred through SSH Stable .

Generate SSH Secret key
ssh-keygen -t rsa -C “[email protected]”

4. View the generated secret key

cat ~/.ssh/id_rsa.pub perhaps gedit ~/.ssh/id_rsa.pub

5.GitHub On the configuration SSH

 Sign in git Official website , website :https://github.com/. Upper right corner After logging in, click settings->SSH and GPS keys->New SSH key

6.windows Create .gitignore file

(git Some files are ignored when submitting )
Is to create a new project under the project directory .gitignore file , Then declare the folder or file to be ignored in it . Here comes the trouble , Because the file starts with a dot , No file name , There's no way to go straight to windows Create . We can install Git, Then right click on the project root directory and select “Git Bash Here”, adopt linux To create new .gitignore file .
The detailed steps are as follows :
install Git, Come first Git Download the latest Git for the Windows platform. When the download is complete , Click on the install , stay “Select Components” Step by step , Remember to choose Simple context menu( Here's the picture ). Others are selected by default , Click Next directly to complete the installation .
Right click the project root directory and select “Git Bash Here” Go to the command line .
Input vim .gitignore command , Edit content
.project
.classpath
.settings
.gitignore
Press wq Save and exit , Will generate .gitignore file . Use again at this time Git Conduct Commit when , You will not see the above excluded files .
.gitignore There are two modes of file filtering , Open model and conservative model
Open mode is responsible for setting which files and folders to filter
settings/ It means to filter this folder
.zip Filter zip Suffix file
Test.java Filter the file
Conservative mode is responsible for setting which files are not filtered , Which files are to be tracked .
!src/ Track this folder
!
.java track java Source file
!Test.java Track this file

Four .sublime

1. default Sublime 3 There is no Package Control

original Subl3 install Package Control so much trouble , Now here's the easy way
Use Ctrl+` 【Tab The symbol above 】 Shortcut key or through View->Show Console Menu open command line , Paste the following code :

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())

With luck , At this time, it can be in Preferences You can see it under the menu Package Settings and Package Control There are two menus .

2.sublime text3 What if there is no intelligent code prompt ?

 Have to say sublime text3 It is a powerful editor , Its main feature is its small size , Fast start . But at the same time, it brings bad side , Is that all “ Special function ” Depend on the installation of plug-ins to improve . therefore sublime text3 No smart code , It is the lack of a plug-in , The following editor will briefly introduce how to make sublime text3 Intelligent code prompt .

(1) Click on “preferences”——“package control” Prepare all operations of plug-in package ;
(2) Click on the “install package” Install the plug-in package ,

(3) Enter in the input box “sublimecodeintel”, Prompt appears , Click to enter the installation immediately ;

(4) Prompt installation , Wait a few seconds ;

(5)、 Installation successful , Return to the prompt ;

(6)、 You can also check whether there is a plug-in package in the menu bar , If yes, the installation is successful ;

3.sublime To configure python3

1. Create a new file named python3.sublime-build
2. Save in 【Preferences】-》【Browse Package】-》【User】 Next
3. Put the following cmd in python Change the path to your computer . Copy to the above file

{"cmd": ["C:\\ProgramData\\Anaconda3\\python.exe", "-u", "$file"],"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)","selector": "source.python","encoding": "cp936"}

4.ctrl+B Whether the test runs successfully

5、 ... and .windows Related settings

5.1 Folder sharing

 Q: There is no right-click attribute in my folder “ share ” But there are “ Security ”, I can't see anything in the folder tab “ Simple file sharing ” A: Turn on Server service : Start - function , Input services.msc, Find the following server service , First set it to automatic , application , Then enable the service 

5.2 Set up cmd The command line can be copied and pasted

win+R Open the command line
Input cmd, And return
Right click the top border , choice 【 attribute 】, Check 【 Quick Edit mode 】
Replication time , Select the content to copy , Press 【enter】 Copy successfully
【 When pasting 】, Right click , It will automatically paste the copied content

5.3 After changing the drive letter, the current path is not displayed

 cd /d D:\cui

5.4 Open hidden folder

Want to turn on the computer C:\Users\Administrator.-20171116XTDXHT\AppData\Roaming Folder discovery is hidden

 stay cmd Command box input %appdata%

5.5 CMD command

mstsc Remote connection dcomcnfg Open system component services regedit Check the registry services.msc system service notepad Open Notepad calc Start the calculator 

6、 ... and .Pytorch edition 、CUDA The corresponding relationship between version and graphics card driver version

1.CUDA Drive and CUDAToolkit Corresponding version

notes : The driver is downward compatible , It determines what can be installed CUDA and CUDAToolkit Highest version .
Insert picture description here

2.CUDA And its availability PyTorch Corresponding version ( Refer to the official website , Welcome to the comments area to add )

Insert picture description here

3. Installation instructions

(1) Specify installation PyTorch edition
When known CUDA version , According to table 2 Directly query the corresponding version PyTorch, function conda install pytorch=X.X.X -c pytorch You can install the specified version PyTorch. This order is made by conda Decision and PyTorch Corresponding CUDAToolkit. But there is no guarantee PyTorch Normal use ,CUDAToolkit The version is not suitable for the graphics card driver , Can lead to CUDAToolkit Version higher than CUDA drive .
(2) Appoint CUDAToolkit edition
The first run nvidia-smi Inquire about CUDA Driver version , According to 1 Query the corresponding CUDAToolkit edition , Run again conda install pytorch cudatoolkit=X.X -c pytorch You can install the specified CUDAToolkit Version of PyTorch.
(3) At the same time specified PyTorch and CUDAToolkit edition
If you are quite sure CUDA Version and corresponding PyTorch and CUDAToolkit The corresponding version can run

conda install pytorch=X.X.X cudatoolkit=X.X -c pytorch

After installation, you can use python see

import torchprint(torch.__version__)print(torch.cuda.is_available())

author : Mr. Deka's mailbox

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved