python为所有用户安装_分发大型复杂Python项目以便为其他用户完全安装的最佳方式,无论操作系统如何?...

如何确保分发具有大依赖性的Python包(如NumPy和SciPy),无论其操作系统如何,都会自动在用户的计算机上正确安装?

问题是许多这样的软件包通过pip的主要渠道或多或少地安装,包括上面提到的那些…

解决方法:

我并不认为这是完美的,但下面是我在实践中发现的非常强大的东西:一个install.sh脚本,最初源于一个名为words2map的人工智能库.目的是确保用户可以只需运行./install.sh然后所有需要的pip就可以安装,并且pip install在每种情况下都会成功(即使对于全新的服务器),无论您使用何种操作系统. (我真的希望操作系统不可知的安装是行业标准,至少在Mac OSX和每个Linux发行版中都是……)

可以直接跳到代码,但对于那些需要解释的人:这个install.sh脚本在很大程度上依赖于Conda软件包分发管理器,它可以很好地工作,因为它可以自动构建特定于操作系统的轮子.下面的安装代码首先检查用户的操作系统,然后安装相应的Conda发行版(如果在Mac OSX和Linux上,在这种情况下),最后通过Conda根据需要安装所有剩余的依赖项.另请注意,如果检测到Linux终端,则首先确保所需的编译库可通过例如sudo apt-get install python-dev(因为用户经常缺少gcc或其他东西),最后这会自动将Conda添加到用户的PATH中以获取bash和zshell,这似乎涵盖了大多数终端(但可能没有,请随意添加) ).很多花里胡哨的东西,但实际上这提供了一个非常好的用户体验,可以帮助用户立即启动和运行您正在分发的新的复杂Python库,因为您可以通过尝试立即下载并运行words2map来测试自己.如果它们有意义/使每个人都更好/更清楚,那么强烈鼓励对此进行编辑.好的,到了节目!

#!/bin/bash

download_miniconda() {

echo "Downloading Miniconda for Python dependencies..."

OS_BIT_TYPE="$(uname -m)"

OS_ARCHITECTURE="$(uname -s)"

if [ $OS_BIT_TYPE == "i686" ]; then

OS_BIT_TYPE="x86"

fi

if [ $OS_ARCHITECTURE == "Darwin" ]; then

OS_ARCHITECTURE="MacOSX"

fi

MINICONDA_INSTALL_FILE="Miniconda2-latest-$OS_ARCHITECTURE-$OS_BIT_TYPE.sh"

MINICONDA_DOWNLOAD_URL="https://repo.continuum.io/miniconda/$MINICONDA_INSTALL_FILE"

$(curl -O $MINICONDA_DOWNLOAD_URL)

$(chmod +x $MINICONDA_INSTALL_FILE)

}

install_miniconda() {

echo "Installing Miniconda..."

echo "$(./$MINICONDA_INSTALL_FILE -b -p $HOME/miniconda)"

echo "$(rm $MINICONDA_INSTALL_FILE)"

}

confirm_miniconda_installed() {

if hash conda 2>/dev/null; then

echo "Miniconda installed!"

else

echo "Failed to install Miniconda. Please visit http://conda.pydata.org/docs/install/quick.html to install and then try rerunning this script, making sure that Miniconda is accessible in the PATH"

fi

}

update_script_startup_file() {

echo "if [[ \":\$PATH:\" != *\":\$HOME/miniconda/bin:\"* ]]; then" >> $STARTUP_FILE

echo " export PATH=\"\$PATH:\$HOME/miniconda/bin\"" >> $STARTUP_FILE

echo "fi" >> $STARTUP_FILE

}

add_miniconda_to_path() {

# temporary update to PATH for this script

export PATH="$PATH:$HOME/miniconda/bin"

# permanent update to PATH for user's convenience

if [ -n "`$SHELL -c 'echo $BASH_VERSION'`" ]; then

STARTUP_FILE="$HOME/.bashrc"

update_script_startup_file

elif [ -n "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then

STARTUP_FILE="$HOME/.zshrc"

update_script_startup_file

else

echo "Couldn't automatically add Miniconda to the PATH of your preferred terminal. We suggest working from Bash or ZShell."

fi

}

install_conda_if_needed() {

if hash conda 2>/dev/null; then

echo "Miniconda installed!"

else

if ping -c 1 google.com >> /dev/null 2>&1; then

download_miniconda

install_miniconda

add_miniconda_to_path

confirm_miniconda_installed

else

echo "Looks like you're offline! Please address this and then try rerunning this script."

fi

fi

}

create_conda_environment() {

if hash conda 2>/dev/null; then

CONDA_ENVIRONMENTS="$(conda env list)"

if [[ "$CONDA_ENVIRONMENTS" != *"words2map"* ]]; then

conda create --name words2map --yes cython scikit-learn gensim seaborn

fi

fi

}

install_developer_libraries_as_needed() {

OS_ARCHITECTURE="$(uname -s)"

if [ $OS_ARCHITECTURE == "Linux" ]; then

echo "$(python -mplatform | grep -qi Ubuntu && sudo apt-get update && sudo apt-get install python-dev || sudo yum update -y && sudo yum install python-devel -y && sudo yum groupinstall "Development Tools" -y)"

fi

}

install_python_dependencies() {

if hash conda 2>/dev/null; then

echo 'Installing Python dependencies for words2map...'

source activate words2map

install_developer_libraries_as_needed

pip install hdbscan pattern semidbm nltk unidecode

source deactivate

fi

}

refresh_user_shell() {

if [ -n "`$SHELL -c 'echo $BASH_VERSION'`" ]; then

exec bash

elif [ -n "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then

exec zsh

fi

}

install_conda_if_needed

create_conda_environment

install_python_dependencies

refresh_user_shell

标签:python,pip,operating-system,conda

来源: https://codeday.me/bug/20190623/1268412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值