1. Why use a Python virtual environment
In actual work, the versions of Python and related web frameworks that we contact are often different. For example, some combinations may be python2 + Django1.8, and some may be python3.6 + Django1.12. Both are easy to handle. , I installed two versions of python on my computer, wouldn't it be fine?
But what if there are five, six, seven or eight projects that require five, six, or eight sets of python environments? This is where the virtual environment comes into play.
2. Download and install
- Mirror of Tsinghua [ https://mirror.tuna.tsinghua.edu.cn/help/anaconda/ ]
- Official instructions [ http://conda.pydata.org/docs/using/envs.html ]
- Download the second miniconda and enter the latest version that matches your computer system.
2.1 Windows
Download the installation package directly, after downloading, just install it by default in the next step.
Install miniconda, default + select environment variables
3 How to Play - Basic Commands
3.1 Basic command description
(Windows is best used with ConEmu)
Relevant files are here:
Tool link: https://pan.baidu.com/s/1i5HtBFn Password: kw4f
Go to Baidu to directly search for ConEmu to download and install the version, (if you can't use it, you can download the green version)
replace cmd
Stay home by default
Replace it with conemu, download the green version, unzip it and use it
Windows is best used with ConEmu
Then configure the window environment
Configure shortcut command aliases
Go to the upper right corner of conemu:
Copy and paste the following variables into the environment variables in settings->Startup>Environment
alias cl=conda env list
alias cc=conda create -n $*
alias coa=activate $*
alias cod=deactivate
alias cr=conda remove --all -n $*
1 li nu x or m a c
# alias for conda
alias cl="conda env list"
alias cc="conda create -n"
alias coa="source activate" alias cod="source deactivate" alias cr="conda remove --all -n"
Note: For Linux, it is best to add environment variables yourself, and you will be reminded when the conda script is executed.
Add the following line to .bashrc,
export PATH=/root/anaconda3/bin:$PATH
Don't forget to reload at the end.
source ~/.bashrc
Go to conemu to continue the operation
#查看版本
conda info —envs
或
conda env list
简写cl
(# **注意,为了避免你的网络太慢,请先运行下面命令加速一些**
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes)
4: Multiple environment demos:
python2
Enter in conemu
> conda create -n py2 python=2
重新开一个再输入
python3.5
- >cc py35 python=3.5
-
Schematic diagram of the new virtual environment:
Then coa + environment name is to enter the environment - After entering the environment, pip freeze to view the current environment (packages installed)
- Naming rules: py version - main program library version number
- For example: cc py2-dj10 python=2 django=1.10 installation command + name (py version plus main library version number) + which environment + install django version
- What to install in the created environment is pip + install + django and it will be installed automatically. This is to install django, the others are equivalent
- cod exit environment
- Exit first to delete, cr + file name
- The international common name of the requirements.txt file is essentially a txt name that contains some libraries. For convenience, it is changed to env.txt
Build method pip freeze> env.txt Enter
Environment migration and import First create an env.txt file, pip freeze>env.txt
The second step of import: pip install -r env.txt
Uninstall method: pip uninstall djamgo
Use multiple python environments with vscoude
Environment variable: first find the location of conemu64 to find the application , copy a copy of conemu64, and change its name to cmdd, then add the location directory of cmdd to the computer's environment variable as follows C:\Users\Administrator\AppData\Local\Programs\ Python\Python36-32\Scripts\;C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\;C:\Program Files\Microsoft VS Code\bin\;C:\Users\Administrator\AppData \Local\Programs\Python\Python36-32\Scripts\;D:\360dowldluo\ConEmuPack.161206\
pycharm download and run
Shortcut operation of pycharm
Introduction to jupyter
What is jupyter?
This starts with ipython. ipython is an interactive python interpreter with its own colors, completions and line numbers. Many big names in the scientific community are used for data analysis and graphic display.
ipython can also run on the browser, it looks like this:
The name is also a little taller, called ipythoon notebook. The jupyter icon was there at the beginning, and now it has been upgraded, not only running python, but also tall things such as R and spark. Therefore, jupyter is used directly to refer to this pair of products.
The official has a try page, you can play.
jupyter installation
The official recommended installation is this: http://jupyter.readthedocs.io/en/latest/install.html
Download Anaconda. We recommend downloading Anaconda’s latest Python 3 version (currently Python 3.5).
Download Anaconda. We recommend downloading Anaconda’s latest Python 3 version (currently Python 3.5).
Install the version of Anaconda, which you downloaded.
Install Jupyter using conda from the Terminal (Mac and Linux) or a Command Prompt window (Windows):
conda install jupyter
Congratulations. You have installed Jupyter Notebook. To run the notebook:
jupyter notebook
We can install it directly
if we already have a python environment:
directpip install jupyter
If not:
install a python environment first, then install
run
jupyter notebook
Then automatically open the 8888 port of localhost in the browser, and you can write code online! Not only python, but also R, etc...
User Interface and Main Features
- write the code
- Write documents (the cell type is divided into markdown and code, you can change it at will, so I write this article directly)
- Scientific operations and drawing (numpy, scipy, pandas, etc. used to need to be installed one by one, and now they are all complete)
sample code
4+6
10
It's a decorator
def show_output(func): def wrapped(*args, **kwargs): output = func(*args, **kwargs) print("the result is : ", output) return wrapped
def is_even(num): return num % 2 ==0
Run the function with the decorator and output the result
f = show_output(is_even)
f(3)
the result is : False
text
To organize useful settings:
Customize the jpython working directory
When using jupyter notebook
the page opened by default, the default is the home path, and you will see a lot of things. What if we only want to display the project directory of jupyter?
Simply put, how to open a custom ipython working directory!
Simple, change the configuration!
The configuration file is in .jupyter\jupyter_notebook_config.py
First find the jupyter_notebook_config.py file on the computer and open it with vscound
Of course, the default may not, you need to manually generate:
jupyter notebook --generate-config
Then open jupyter_notebook_config.py and find
c.NotebookApp.notebook_dir = u'C:\\study\\jupyter'
Add your directory and start the notebook again. Here are the settings under my win7. You are free.
jupyter notebook
Restart, create a new file again, and you will find that it is in this custom project directory.
If there is no directory, then create it, cd.. until the c drive and then mkdir study----this is to create a directory, then enter this directory cd study+tab to enter the study directory, and then mkdir jupyter
Then there is jupyter notebook
two modes
Command Mode (Esc)
In the default input mode, press Esc
to enter the command mode, is it similar to vim?
Code and markdown are the two commonly used ones: use y和m
toggles respectively, provided that they are in command mode!
View shortcut keys:
Help-Keyboard Shortcuts
As shown in the figure below: I will add some color to the most commonly used ones!
Input mode (Enter)
Those who have used ipython should be familiar with the previous one In[]
. This kind of sign has been used to directly enter the code.
This kind of code has statements, running results, and error prompts. It is a must-have at home!
The following is an example of detecting the python version:
but at this point we are writing, how to add a cell and start writing code until it runs?
Proceed as follows:
- Esc to enter command mode
- Press B to add a line below
- write the code
- Use ctrl+enter to run, or alt+enter (this will automatically insert another line)
- You will see something like the following
import sys
print sys.version
2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]
Jupyter is great fun - debugging code automatically changes to documentation
text
Sometimes, when we write python programs, there will always be various mistakes. When the wine has passed three rounds, the dishes have passed five flavors, and all the problems have been solved, we want to summarize the mistakes we have made for future inquiries. At this time, the problem comes, do you want to reproduce it one by one?
Jupyter is here! Everything is solved, this is a code camera, there is an error when writing the code, a bunch of error messages, it doesn't matter, insert a new cell and write it again! until it's right!
first mistake
# 假装我不会写helloworld
print hello world
File "<ipython-input-1-e9edef3fb57e>", line 2
print hello world
^
SyntaxError: invalid syntax
second mistake
# 难道加个引号?
print 'hello world"
File "<ipython-input-2-efbc3c88a80d>", line 2
print 'hello world"
^
SyntaxError: EOL while scanning string literal
what's going on? Not even quotes? !
Well, it turned out to be a pair!
finally
# 这次对了吧?你好!世界!
print 'hello world'
hello world
ok, write here, the error code is there, the error message is there, and finally the correct code is there, let's publish a blog!
Publish with vscode
- save as md file
- vscode open
- Publish with the writecnblog plugin
text
Sometimes, when we write python programs, there will always be various mistakes. When the wine has passed three rounds, the dishes have passed five flavors, and all the problems have been solved, we want to summarize the mistakes we have made for future inquiries. At this time, the problem comes, do you want to reproduce it one by one?
Jupyter is here! Everything is solved, this is a code camera, there is an error when writing the code, a bunch of error messages, it doesn't matter, insert a new cell and write it again! until it's right!
first mistake
# 假装我不会写helloworld
print hello world
File "<ipython-input-1-e9edef3fb57e>", line 2
print hello world
^
SyntaxError: invalid syntax
second mistake
# 难道加个引号?
print 'hello world"
File "<ipython-input-2-efbc3c88a80d>", line 2
print 'hello world"
^
SyntaxError: EOL while scanning string literal
what's going on? Not even quotes? !
Well, it turned out to be a pair!
finally
# 这次对了吧?你好!世界!
print 'hello world'
hello world
ok, write here, the error code is there, the error message is there, and finally the correct code is there, let's publish a blog!
Publish with vscode
- save as md file
- vscode open
- Publish with the writecnblog plugin