current position:Home>The first step of scientific research: create Python virtual environment on Linux server

The first step of scientific research: create Python virtual environment on Linux server

2022-01-31 01:29:40 No front


Why create python A virtual environment

Blog for the first time , I want to write a blog to record my graduate study , Answer title , After entering the laboratory , If necessary, often in github Run other people's code or baseline When , Based on the present AI Mostly by python Realization , But when running the experiment, because python Version and tensorflow as well as keras It's a headache when all kinds of libraries are incompatible , Therefore, we need to realize the rapid construction of a python Virtual environments allow you to run fast baseline Model , And debug and learn .

How to achieve

First step GPU Drive installation

  1. Check the server linux Version and GPU Information , Use the following command :

 Look at... On the server GPU Information 2. According to the system version and GPU Find the right graphics card driver and download and install NVIDIA Graphics card driver installation website linux Installation instructions

wget -c
 Copy code 

test : Input nvidia-smi, If there is a table output, it indicates that the driver installation is successful , as follows :  The graphics card driver is installed successfully

The second step anaconda Realize the creation of virtual environment

Why choose anaconda

Anaconda Can help us create multiple development environments , It can also help us install third-party packages . For example, in the installation tensorflow When , It will help us install many other supporting packages , So that version compatibility problems will not occur

install anaconda Instructions

  1. Download installation package

Here we use Tsinghua image file to download faster

 Copy code 
  1. install
 Copy code 
  1. Update environment variables
source ~/.bashrc
 Copy code 

establish python A virtual environment

conda establish

anaconda After successful installation, you can use conda Directive to create a virtual environment Establish environmental directives : conda create -n name python=3.6 name That is, the name of the virtual environment you want to create ,python After that, you can enter what you want to install python edition , If github The above code will appear python2.x When it comes to the situation , You can also install python2.x edition Example :conda create -n tf27 python=2.7  Create a virtual environment example Pictured above , Created a python=2.7, The name is tf27 Virtual environment for

conda Activate

After creating the environment, you need to activate , Use instruction conda activate name name Name the virtual environment you just named yourself Example :conda activate tf27  A virtual environment At this time, notice that the brackets on the left have changed from base Turned into tf27, That is, we are from base The environment is switched to tf27 In this virtual environment , Any installed in this virtual environment python Libraries are only valid for this virtual environment , That is, we can realize the only virtual environment and configure the library version we want , Be careful : The packages configured in this virtual environment are base It can't be used in the environment , But we don't usually base Environment running code . If you want to switch back to base The environment only needs to use instructions

conda deactivate
 Copy code 


stay base Enter... In the environment conda env list, You can view which virtual environments are currently installed , To delete a virtual environment , need conda env remove -n ( Name of the environment )

Install in the current virtual environment tensorflow-gpu

Just use conda Command is enough ,

conda install tensorflow-gpu
 Copy code 

If you need to select a version , It's fine too conda install tensorflow-gpu=xxx But do it conda Will detect the to be installed tf Whether the version is the same as the current version python Version compatibility , If incompatible, refuse to install

verification tf install

Input python You will see the current python Version information for stay python Enter... In the editing environment import tensorflow as tf as well as tf.__version__ You can see tf Version information for


Attach frequently forgotten installation instructions : pytorch-cuda Version installation : pytorch Download the official website version Select the corresponding adapter , You can use different instructions  Insert picture description here What often happens is python3.6 Of pytorch

conda install pytorch==1.6.0 torchvision==0.7.0 -c pytorch
 Copy code 


thus , We finished in linux Build a virtual environment on the server , And you can install any version of python and tensorflow, adopt anaconda In theory, we can create many virtual environments , Different versions are required for running out python And the code of the deep learning framework , But in order to run the code more conveniently , We usually use pycharm Connect to the server or jupyter Combined with the things used in this blog , Implement graphical editing interface and run code on the server , Fast implementation , Many environments , Write code environment with good appearance and other advantages , It makes it more convenient to run models in different virtual environments , The next blog will be about pycharm Connect to the server and jupyter Use .

copyright notice
author[No front],Please bring the original link to reprint, thank you.

Random recommended