700 字
4 分钟
Conda 和 pip 换源
Conda 和 pip 换源
conda
中科大源
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 yesconda config --remove-key channels # 换回默认源
conda env remove --name your_env_name # 删除环境PIP
下载某一个文件的时候,临时使用中科大源,加上如下后缀就行
pip install xx -i https://pypi.mirrors.ustc.edu.cn/simple/永久
pip install pip -U在Jupyter笔记本中直接运行C语言代码并不直接支持,因为Jupyter是为Python、Julia、R和其他支持Jupyter内核的语言设计的。但是,你可以通过一些方法间接地在Jupyter笔记本中运行C语言代码:
#delete the envconda remove -n lglc --all -y
# create the envirment for cconda create -n lglc python==3.11conda activate lglc
# change the conda sourceconda 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代码的步骤:
- 首先,编写C代码并保存到文件中。
#include <stdio.h>
int main() { printf("Hello, World!\n"); return 0;}- 使用
!前缀运行命令来编译C代码。
!gcc hello.c -o hello- 使用
!前缀运行编译后的程序。
!./hello使用%%script魔法命令
Jupyter还提供了%%script魔法命令,允许你在笔记本中运行脚本。这同样需要系统上安装了C编译器。
%%script bashgcc hello.c -o hello && ./hello使用%%writefile魔法命令
你可以使用%%writefile魔法命令将代码块写入文件,然后编译和运行它。
%%writefile hello.c#include <stdio.h>
int main() { printf("Hello, World!\n"); return 0;}接着编译并运行:
!gcc hello.c -o hello!./hello使用ctypes或cffi库
如果你想在Python代码中调用C函数,你可以使用ctypes或cffi库。这需要你创建一个C库,然后从Python中加载它。
这是一个使用ctypes的简单例子:
- 编写C代码并编译为共享库。
#include <stdio.h>
void say_hello() { printf("Hello from C!\n");}编译为共享库:
!gcc -shared -o mylib.so -fPIC mylib.c- 使用
ctypes加载并调用C函数。
from ctypes import cdll
# 加载共享库mylib = cdll.LoadLibrary('./mylib.so')
# 调用C函数mylib.say_hello()以上方法可以帮助你在Jupyter笔记本中运行C语言代码或调用C函数。记住,这些方法需要在你的系统上安装C编译器,并且它们不会在Jupyter笔记本内部执行C代码,而是通过外部命令或库调用。