700 字
4 分钟
Conda 和 pip 换源
2023-09-12

Conda 和 pip 换源#

conda#

中科大源

Terminal window
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes
conda config --remove-key channels # 换回默认源
conda env remove --name your_env_name # 删除环境

PIP#

下载某一个文件的时候,临时使用中科大源,加上如下后缀就行

Terminal window
pip install xx -i https://pypi.mirrors.ustc.edu.cn/simple/

永久

Terminal window
pip install pip -U

在Jupyter笔记本中直接运行C语言代码并不直接支持,因为Jupyter是为Python、Julia、R和其他支持Jupyter内核的语言设计的。但是,你可以通过一些方法间接地在Jupyter笔记本中运行C语言代码:

Terminal window
#delete the env
conda remove -n lglc --all -y
# create the envirment for c
conda create -n lglc python==3.11
conda activate lglc
# change the conda source
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes
# change the pip source
pip install pip -U
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple
# insall jupyter notebook
pip install numpy matplotlib jupyter

使用!前缀运行C代码#

你可以使用Jupyter的shell命令功能来编译和运行C代码。这需要你已经在系统上安装了C编译器(如GCC)。

以下是如何在Jupyter笔记本中使用!前缀来编译和运行C代码的步骤:

  1. 首先,编写C代码并保存到文件中。
hello.c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
  1. 使用!前缀运行命令来编译C代码。
Terminal window
!gcc hello.c -o hello
  1. 使用!前缀运行编译后的程序。
Terminal window
!./hello

使用%%script魔法命令#

Jupyter还提供了%%script魔法命令,允许你在笔记本中运行脚本。这同样需要系统上安装了C编译器。

Terminal window
%%script bash
gcc hello.c -o hello && ./hello

使用%%writefile魔法命令#

你可以使用%%writefile魔法命令将代码块写入文件,然后编译和运行它。

%%writefile hello.c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}

接着编译并运行:

Terminal window
!gcc hello.c -o hello
!./hello

使用ctypes或cffi库#

如果你想在Python代码中调用C函数,你可以使用ctypes或cffi库。这需要你创建一个C库,然后从Python中加载它。

这是一个使用ctypes的简单例子:

  1. 编写C代码并编译为共享库。
mylib.c
#include <stdio.h>
void say_hello() {
printf("Hello from C!\n");
}

编译为共享库:

Terminal window
!gcc -shared -o mylib.so -fPIC mylib.c
  1. 使用ctypes加载并调用C函数。
from ctypes import cdll
# 加载共享库
mylib = cdll.LoadLibrary('./mylib.so')
# 调用C函数
mylib.say_hello()

以上方法可以帮助你在Jupyter笔记本中运行C语言代码或调用C函数。记住,这些方法需要在你的系统上安装C编译器,并且它们不会在Jupyter笔记本内部执行C代码,而是通过外部命令或库调用。

Conda 和 pip 换源
https://tuoyuangui.github.io/website/posts/typora/python/conda-pip-换源/
作者
椭圆规/tuoyuangui
发布于
2023-09-12
许可协议
CC BY-NC-SA 4.0