python笔记6-模块和虚拟环境

新建helpers.py文件

#helpers.py
def display(message,is_warning=False):
    if is_warning:
        print('Warning!!')
    print(message)

几种导入方法 

#test.py
import helpers
helpers.display('Not a warning')

from helpers import *
display('Not a warning')

from helpers import display
display('Not a warning')
'''
Not a warning
Not a warning
Not a warning
'''

pip下载网上已封装好的package,pip install是全局下载,有时会碰到版本问题

设置vscode的python虚拟环境可以隔开不同版本,虚拟环境就是一个文件夹,里面包含程序所需要的所有代码,只要把所需要的东西都安装进去就能使用它

创建这个文件夹的步骤:

1.pip install virtualenv

2.Window系统:python -m venv <文件夹名字>

   OSX/linux(bash):virtualenv <文件夹名字>

(在网上搜索看到有人说不能有中文路径)

3.激活

#Windows system

#cmd.exe

<folder_name>\Scripts\Activate.bat

#Powershell

<folder_name>\Scripts\Activate.ps1

#bash shell

. ./<folder_name>/Scripts/activate

#OSX/LInux(bash)

<folder_name>/bin/activate

设置虚拟环境(python -m venv venv)->激活(. \venv\Scripts\active.ps1),可以看到一个绿色的(venv)在行首,或者vscde左下脚出现venv,使用的环境名称->设置一个requirements.txt,里面写要用到的包列表->pip install -r requirements.txt

-m: run library module as a script(将模块当作脚本运行)

猜你喜欢

转载自blog.csdn.net/qq_40431700/article/details/107098390
今日推荐