1.安装virtualenv
cd ~ pip list #查看pip安装列表 pip install virtualenv
2.创建文件夹,在里面安装虚拟环境
mkdir .venvs
virtualenvs --system-site-package .venvs/lpthw
3.激活虚拟环境
PS C:Usersxxxx> ..venvslpthwScriptsactivate
(lpthw) PS C:Usersxxxx>
在powershell执行脚本时报错,原因是powershell默认执行策略为Restricted,不支持运行脚本,可以对策略进行更改
PS C:Usersxxxx> ..venvslpthwScriptsactivate ..venvslpthwScriptsactivate : 无法加载文件 C:Userscrystaldu.venvslpthwScriptsactivate.ps1,因为在此系统上禁止 运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1 + ..venvslpthwScriptsactivate + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
①查询当前策略
PS C:Usersxxxxx> get-executionpolicy
②更改当前策略为remotesigned,关于更多执行策略可以参考:https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-6
PS C:Usersxxxx> set-executionpolicy remotesigned
4.创建骨架项目目录
mkdir -p projects/skeleton/bin #创建一个多级目录,-p 用/隔开 cd projects/skeleton/bin mkdir -p bin,NAME,tests,docs #创建多个同级目录,-p 用,隔开
5.设置初始文件
(lpthw) PS C:Usersxxxprojectsskeleton> new-item -type file NAME/__init__.py (lpthw) PS C:Usersxxxprojectsskeleton> new-item -type file tests/__init__.py
6.安装文件 setup.py 【C:Usersxxxxprojectsskeleton】
try: from setuptools import setup except ImportError: from distutils.core import setup config = { 'description': 'My Project', 'author': 'xxxx', 'url': 'https://www.cnblogs.com/ohlala/', 'download_url': 'xxxxx', 'author_email': 'xxxx@163.com', 'version': '0.1', 'install_requires': ['nose'], 'packages':['NAME'], 'scripts':[], 'name': 'projectname' } setup(**config)
创建NAME_tests.py 【C:Usersxxxxprojectsskeleton ests】
from nose.tools import * import NAME def setup(): print("SETUP!") def teardown(): print("TEAR DOWN!") def test_basic(): print("I RAN!", end='')
7.复制项目骨架
(lpthw) PS C:Usersxxxprojects> cp -r .skeleton ex47 (lpthw) PS C:Usersxxxprojectsex47> ls 目录: C:Usersxxxprojectsex47 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2019/9/4 10:54 bin d----- 2019/9/4 10:54 docs d----- 2019/9/4 10:54 NAME d----- 2019/9/4 10:54 tests -a---- 2019/9/4 10:17 469 setup.py (lpthw) PS C:Usersxxxprojectsex47> mv NAME ex47 (lpthw) PS C:Usersxxxprojectsex47> ls 目录: C:Usersxxxprojectsex47 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2019/9/4 10:54 bin d----- 2019/9/4 10:54 docs d----- 2019/9/4 10:54 ex47 d----- 2019/9/4 10:54 tests -a---- 2019/9/4 10:17 469 setup.py
(lpthw) PS C:Usersxxxprojectsex47> mv . estsNAME_tests.py ex47_tests.py
然后修改ex47_tests.py中的代码
from nose.tools import * import ex47 def setup(): print("SETUP!") def teardown(): print("TEAR DOWN!") def test_basic(): print("I RAN!", end='')
修改后执行nosetests进行测试
(lpthw) PS C:Userscrystalduprojectsex47> nosetests . ---------------------------------------------------------------------- Ran 1 test in 0.037s OK (lpthw) PS C:Userscrystalduprojectsex47> nosetests -s SETUP! .I RAN!TEAR DOWN! ---------------------------------------------------------------------- Ran 1 test in 0.031s OK
8.安装
(lpthw) PS C:Usersxxxprojectsex47> python .setup.py install running install running bdist_egg running egg_info creating ex47.egg-info writing ex47.egg-infoPKG-INFO writing dependency_links to ex47.egg-infodependency_links.txt writing requirements to ex47.egg-info equires.txt writing top-level names to ex47.egg-info op_level.txt writing manifest file 'ex47.egg-infoSOURCES.txt' reading manifest file 'ex47.egg-infoSOURCES.txt' writing manifest file 'ex47.egg-infoSOURCES.txt' installing library code to builddist.win-amd64egg running install_lib running build_py creating build creating buildlib creating buildlibex47 copying ex47__init__.py -> buildlibex47 creating builddist.win-amd64 creating builddist.win-amd64egg creating builddist.win-amd64eggex47 copying buildlibex47__init__.py -> builddist.win-amd64eggex47 byte-compiling builddist.win-amd64eggex47__init__.py to __init__.cpython-37.pyc creating builddist.win-amd64eggEGG-INFO copying ex47.egg-infoPKG-INFO -> builddist.win-amd64eggEGG-INFO copying ex47.egg-infoSOURCES.txt -> builddist.win-amd64eggEGG-INFO copying ex47.egg-infodependency_links.txt -> builddist.win-amd64eggEGG-INFO copying ex47.egg-info equires.txt -> builddist.win-amd64eggEGG-INFO copying ex47.egg-info op_level.txt -> builddist.win-amd64eggEGG-INFO zip_safe flag not set; analyzing archive contents... creating dist creating 'distex47-1.0-py3.7.egg' and adding 'builddist.win-amd64egg' to it removing 'builddist.win-amd64egg' (and everything under it) Processing ex47-1.0-py3.7.egg Copying ex47-1.0-py3.7.egg to c:usersxxx.venvslpthwlibsite-packages Adding ex47 1.0 to easy-install.pth file Installed c:usersxxx.venvslpthwlibsite-packagesex47-1.0-py3.7.egg Processing dependencies for ex47==1.0 Searching for nose==1.3.7 Best match: nose 1.3.7 Adding nose 1.3.7 to easy-install.pth file Installing nosetests-script.py script to C:Usersxxx.venvslpthwScripts Installing nosetests.exe script to C:Usersxxx.venvslpthwScripts Installing nosetests-3.4-script.py script to C:Usersxxx.venvslpthwScripts Installing nosetests-3.4.exe script to C:Usersxxx.venvslpthwScripts Using c:usersxxx.venvslpthwlibsite-packages Finished processing dependencies for ex47==1.0 (lpthw) PS C:Usersxxxprojectsex47>
9.卸载
(lpthw) PS C:Usersxxxprojectsex47> pip uninstall ex47 Uninstalling ex47-1.0: Would remove: c:usersxxx.venvslpthwlibsite-packagesex47-1.0-py3.7.egg Proceed (y/n)? y Successfully uninstalled ex47-1.0 (lpthw) PS C:Usersxxxprojectsex47>