windows下的开发环境搭建-python/nodejs

本文详细介绍了如何在Windows环境下使用PowerShell和Chocolatey安装Python和Node.js,包括设置执行策略、安装和升级软件包、创建虚拟环境以及配置环境变量等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

有些时候,我可能误解windows了。大概是因为我的无知吧!

python

安装python

win+s 快捷搜索开打powershell 右击以管理员执行
# 开启powershell执行策略
set-executionpolicy remotesigned
# 安装choco
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
# 升级choco
choco upgrade chocolatey
# 安装python
choco install -y python3
# 关闭终端,刷新环境变量,查看版本
python -v

安装pip

python -m pip install --upgrade pip
pip install --upgrade pip

虚拟环境

mkdir myproject
cd myproject
python -m venv my_env
ls my_env
my_env\Scripts\activate

常用

 choco install openssh git -y

nodejs

# 0.vars
$normal_node="node-v10.14.1-win-x64"
$normal_node_zip=$normal_node+".zip"
$latest_node="node-v11.4.0-win-x64"
$latest_node_zip=$latest_node+".zip"
$npm_verson="v6.2.0-next.1"
$normal_node_zip_url="https://nodejs.org/dist/v10.14.1/node-v10.14.1-win-x64.zip"
$latest_node_zip_url="https://nodejs.org/dist/v11.4.0/node-v11.4.0-win-x64.zip"
$npm_zip_url="https://github.com/npm/npm/archive/v6.2.0-next.1.zip"
$nodedir="c:\NODE"

if ( !(Test-Path $nodedir))
{
md $nodedir
}

# 1. create dir
$start = Get-Date
if ( !(Test-Path $nodedir\node))
{
md $nodedir\node
}
if ( !(Test-Path  $nodedir\npm-global))
{
md $nodedir\npm-global
}
if ( !(Test-Path $nodedir\npm-cache))
{
md $nodedir\npm-cache
}
if ( !(Test-Path $nodedir\zipfile))
{
md $nodedir\zipfile
}

# 2. download and ext
if (!(Test-Path $nodedir\zipfile\$latest_node_zip))
{
	$client = new-object System.Net.WebClient
	Write-Host $latest_node_zip_url
	$client.DownloadFile("$latest_node_zip_url", "$nodedir\zipfile\$latest_node_zip")
	if ( !(Test-Path $nodedir\zipfile\$latest_node))
	{
		Expand-Archive -Path $nodedir\zipfile\$latest_node_zip -DestinationPath $nodedir\zipfile\
	}
}


if (!(Test-Path $nodedir\zipfile\$normal_node_zip))
{
	$client = new-object System.Net.WebClient
	$client.DownloadFile("$normal_node_zip_url", "$nodedir\zipfile\$normal_node_zip")
	if ( !(Test-Path $nodedir\zipfile\$normal_node))
	{
		Expand-Archive -Path $nodedir\zipfile\$normal_node_zip -DestinationPath $nodedir\zipfile\
	}
}

if (!(Test-Path $nodedir\zipfile\$npm_verson.zip))
{
	[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls11, Tls, Ssl3"
	Invoke-WebRequest -Uri $npm_zip_url -OutFile "$nodedir\zipfile\$npm_verson.zip"
	if ( !(Test-Path $nodedir\zipfile\$npm_verson))
	{
		Expand-Archive -Path $nodedir\zipfile\$npm_verson.zip -DestinationPath $nodedir\zipfile\
	}
}



$end = Get-Date
Write-Host -ForegroundColor Red ('download and Decompression node.js : ' + ($end - $start).TotalSeconds)

# 3.config nodejs

Copy-Item $nodedir\zipfile\$normal_node\node.exe $nodedir\node\$normal_node.exe
Copy-Item $nodedir\zipfile\$latest_node\node.exe $nodedir\node\$latest_node.exe
if (Test-Path $nodedir\node\node.exe)
{
Move-Item $nodedir\node\node.exe $nodedir\node\"node$(Get-Date -Format 'yyyyMdHms').exe"
}
Move-Item $nodedir\node\$latest_node.exe $nodedir\node\node.exe

# 4.edit path

$nodejspath="$nodedir\node"
$PathVariable= [System.Environment]::GetEnvironmentVariable("Path","Machine")
$PathFolders=$PathVariable.Split(";")
if ($PathFolders.Contains($nodejspath))
{
	Write-Host "$nodejspath already exists"
}
else
{
	if ($PathVariable.Trim().EndsWith(";"))
	{
		$PathVariable = $PathVariable + $nodejspath
	}
	else
	{
		$PathVariable = $PathVariable +";"+ $nodejspath
	}
	[System.Environment]::SetEnvironmentVariable("Path",$PathVariable,"Machine")
	Write-Host "$nodejspath add to Path successfull"
}


$npmpath="$nodedir\NODE\npm-global"
$PathVariable= [System.Environment]::GetEnvironmentVariable("Path","Machine")
$PathFolders=$PathVariable.Split(";")
if ($PathFolders.Contains($npmpath))
{
	Write-Host "$npmpath already exists"
}
else
{
	if ($PathVariable.Trim().EndsWith(";"))
	{
		$PathVariable = $PathVariable + $npmpath
	}
	else
	{
		$PathVariable = $PathVariable +";"+ $npmpath
	}
	[System.Environment]::SetEnvironmentVariable("Path",$PathVariable,"Machine")
	Write-Host "$npmpath  add to Path successfull"
}

参考文章:https://blog.youkuaiyun.com/nakiri_arisu/article/details/82425402

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值