Python Virtual Environment setup

Source: Internet
Author: User
Tags install django pip install django virtual environment virtualenv

"Prerequisite Summary"
Django is also a popular web framework that is much more complex than flask. Django iterations are updated very quickly and frequently, so there are some obsolete things that need to be discarded, and something new needs to be added, resulting in incompatibilities between different versions. such as Django1.3, Django1.4, Django1.8 There is a great difference between.

"Story Background"
Suppose you want to do Python web development, using Django. There are two old items on hand, A and B need maintenance, and new Project C is under development. Here project a uses django1.3, Project B uses django1.4, and the new Project C uses Django1.8. So the question is, how do you develop and maintain ABC three projects at the same time?

The normal pattern might be this: now there is a bug on item A that needs to be repaired, so first execute the following command to remove the original version:

Pip Uninstall Django

Then execute the following command to install django1.3

Pip Install django==1.3

After a few minutes, the bug fix, OK, now the new Project C development, and then repeat the above story.

Well, this is still the ideal situation. What the? Is there a situation that is not ideal? Yes, Django-based third-party dependencies are also related to the Django version, so in addition to the install and uninstall Django, but also uninstall and install its dependencies, ORZ, this special is embarrassing ...

OK, can start the virtual environment of the journey, with the tight, students.

First step: Install the virtual environment

Pip Install Virtualenv

Step Two: Create a virtual environment
We will first create a folder in the root directory of the C drive to put all our virtual environments. such as C:\envs. Next, we go into the console, like in the look:

Next, we create a virtual environment named flask:

Virtualenv Flask

Execute the above command to get the following result:

In the picture I executed the dir command, and I can see that there is a directory named flask in the directory. Don't believe, let's take a look at:

This is called "flask" in the directory, a lot more files, this is a good virtual environment to create.

Step three: Enter/exit the virtual environment
In the virtual environment, there are a few very important commands that are stored in the Scripts directory, in this case, in the C:\envs\flask\Scripts directory, under the length of the following:


We see the familiar pip.exe, which is used to install Python packages in a virtual environment.
There are two more commands Activate.bat/deactivate.bat, two of them are a pair, one is used to enter the virtual environment, one for leaving the virtual environment. Be sure to remember!

Next we install the flask in the virtual environment, such as:

Note: After entering the virtual environment, the file path is preceded by a parenthesis (flask), which proves that you have entered the virtual environment.

OK, next we will build a simple website, minutes, really is divided into minutes.

First introduce flask,flask is a very small framework, founded in 2010, is a relatively young framework, is a more famous Python web open source framework, once mastered it, you can read all its source code, can go directly to github download its source code for reading (flask source).

Flask Although small, but does not mean that it can only do some small demo, some small and medium-sized web site can be done, such as the more famous shell net is the use of flask to achieve.

Flask is also very simple to get started, a function, a route to build the site. For example, we just need to create a new py file (assuming it's called index.py) and put it into the code below to implement a simple Hello World Web page:

FromFlaskImport flaskapp =flask (__name__ @app . ( "/" ) def  Hello ():  return  "Hello world!" if __name__ ==  "__main__" : App. ()               

Then execute in our virtual environment:

Python index.py

Access to the http://127.0.0.1:5000 can be accessed, such as:

See, is it a matter of minutes?

Finally, paste the Flask learning document:
Chinese documents see here: http://docs.jinkan.org/docs/flask/
English document look here: http://flask.pocoo.org/


Navcat
Links: Http://www.imooc.com/article/18537?block_id=tuijian_wz
Source: MU-Class Network
This article original published in Mu class network, reproduced please indicate the source, thank you for your cooperation!

Python Virtual Environment setup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.