1 Star 4 Fork 2

tonybearpan/30-Days-Of-Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
01_Day_Introduction
02_Day_Variables_builtin_functions
03_Day_Operators
04_Day_Strings
05_Day_Lists
06_Day_Tuples
07_Day_Sets
08_Day_Dictionaries
09_Day_Conditionals
10_Day_Loops
11_Day_Functions
12_Day_Modules
13_Day_List_comprehension
14_Day_Higher_order_functions
15_Day_Python_type_errors
16_Day_Python_date_time
17_Day_Exception_handling
18_Day_Regular_expressions
19_Day_File_handling
20_Day_Python_package_manager
21_Day_Classes_and_objects
22_Day_Web_scraping
23_Day_Virtual_environment
23_virtual_environment.md
24_Day_Statistics
25_Day_Pandas
26_Day_Python_web
27_Day_Python_with_mongodb
28_Day_API
29_Day_Building_API
30_Day_Conclusions
data
files
images
mypackage
numpy_files
old_files
python_for_web
test_files
.gitignore
LICENSE
mymodule.py
numpy.md
readme.md
克隆/下载
23_virtual_environment.md 3.89 KB
一键复制 编辑 原始数据 按行查看 历史
Asabeneh 提交于 4年前 . vitrual env

30 Days Of Python: Day 23 - Virtual Environment

Twitter Follow

Author: Asabeneh Yetayeh
Second Edition: July, 2021

<< Day 22 | Day 24 >>

30DaysOfPython

📘 Day 23

Setting up Virtual Environments

To start with project, it would be better to have a virtual environment. Virtual environment can help us to create an isolated or separate environment. This will help us to avoid conflicts in dependencies across projects. If you write pip freeze on your terminal you will see all the installed packages on your computer. If we use virtualenv, we will access only packages which are specific for that project. Open your terminal and install virtualenv

asabeneh@Asabeneh:~$ pip install virtualenv

Inside the 30DaysOfPython folder create a flask_project folder.

After installing the virtualenv package go to your project folder and create a virtual env by writing:

For Mac/Linux:

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project\$ virtualenv venv

For Windows:

C:\Users\User\Documents\30DaysOfPython\flask_project>python -m venv venv

I prefer to call the new project venv, but feel free to name it differently. Let us check if the the venv was created by using ls (or dir for windows command prompt) command.

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ ls
venv/

Let us activate the virtual environment by writing the following command at our project folder.

For Mac/Linux:

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ source venv/bin/activate

Activation of the virtual environment in Windows may very on Windows Power shell and git bash.

For Windows Power Shell:

C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\activate

For Windows Git bash:

C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\. activate

After you write the activation command, your project directory will start with venv. See the example below.

(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$

Now, lets check the available packages in this project by writing pip freeze. You will not see any packages.

We are going to do a small flask project so let us install flask package to this project.

(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip install Flask

Now, let us write pip freeze to see a list of installed packages in the project:

(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip freeze
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

When you finish you should dactivate active project using deactivate.

(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ deactivate

The necessary modules to work with flask are installed. Now, your project directory is ready for a flask project. You should include the venv to your .gitignore file not to push it to github.

💻 Exercises: Day 23

  1. Create a project directory with a virtual environment based on the example given above.

🎉 CONGRATULATIONS ! 🎉

<< Day 22 | Day 24 >>

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/tonybearpan/Thirty-Days-Of-Python.git
git@gitee.com:tonybearpan/Thirty-Days-Of-Python.git
tonybearpan
Thirty-Days-Of-Python
30-Days-Of-Python
master

搜索帮助