【解决方案】系统已经安装pytorch却调用不了,报错ModuleNotFoundError: No module named ‘torch’
引言
云服务器上配置时显示已经有pytorch环境但是运行却报错说没有,这是由于没有进入pytorch所配置的环境造成的,进入对应环境即可运行pytorch
解决方案
首先错误如下:

解决:
Ctrl+Z退出python,输入以下命令确认系统有pytorch
conda env list
如下图所示可以看到pytorch所在的环境名称为Pytorch-gpu

接着输入以下命令进入环境
conda activate Pytorch-gpu
左侧出现(Pytorch-gpu)表示目前在Pytorch-gpu环境下,这是导入torch就没问题了

pytorch 安装
1.cpu下的pytorch 安装
pip install torch
2.gpu下的pytorch 安装
首先进入pytorch官网
往下滑输入自己的对应各个环境的版本,底下那一行就是安装命令,打开命令行输入该命令即可

注:也可以通过以下命令在单独创建一个环境,进入环境安装torch,实现上文中多个环境的效果
创建环境,其中为pytorch1环境名可自定义
conda create -n pytorch1 python=3.9
进入环境
conda activate pytorch1
Original: https://blog.csdn.net/qq_43605229/article/details/124809610
Author: Lin-CT
Title: 【解决方案】系统已经安装pytorch却调用不了,报错ModuleNotFoundError: No module named ‘torch‘
相关阅读
Title: 【Python 基础教程】Python生成随机数
文章目录
- 前言
- 一、随机数种子
- 二、生成随机数
* - 1.random()
- 2.ranint(a,b)
- 3.randrange(start,stop [,step])
- 4.getrandbits(k)
- 三、生成随机序列
* - 1.choice(seq)
- 2.samplex(序列,k)
- 3.shuffle(x[,random])
前言
生成随机数一般使用的就是random模块下的函数,生成的随机数并不是真正意义上的随机数,而是对随机数的一种模拟。random模块包含各种伪随机数生成函数,以及各种根据概率分布生成随机数的函数。今天我们的目标就是摸清随机数有几种生成方式。

–
; 一、随机数种子
为什么要提出随机数种子呢?咱们前面提到过了,随机数均是模拟出来的,
想要模拟的比较真实,就需要变换种子函数内的数值,一般以时间戳为随机函数种子。
例如以下案例,将随机数种子固定的时候,生成的随机数也将固定。
默认情况下,系统使用时间戳作为种子来生成随机数。[En]
By default, the system uses the timestamp as the seed to generate random numbers.
单一时间戳
随机时间戳
第一次结果
第二次结果![]()
二、生成随机数
以下一生成10个1-100的随机数为例
1.random()
生成[0-1)的随机数为float型。后面的大部分函数都是基于这个函数进行随机数生成的
如果要在响应区中生成随机数,可以使用此函数乘以相应的整数[En]
If you want to generate a random number in the response area, you can use this function to multiply by a corresponding integer
from random import *
for i in range(10):
print(int(random()*100+1),end=" ")
print()
2.ranint(a,b)
随机生成一个a-b的整数
from random import *
for i in range(10):
print(randint(1,100),end=" ")
3.randrange(start,stop [,step])
有起始、终止、步长三大要素,在生成随机数的时候包括下限不包括上限。
from random import *
for i in range(10):
print(int(randrange(1,101)),end=" ")
4.getrandbits(k)
返回一个随机整数,整数的位长为k位。
from random import *
for i in range(10):
print(int(getrandbits(4)),end=" ")
三、生成随机序列
1.choice(seq)
从给定的序列中随机抽取一个
代码如下:
from random import *
test=[12,3,1,2,33,21]
for i in range(10):
print(choice(test))
2.samplex(序列,k)
从序列中随机抽取k个元素,这k个元素不会重复。(需要满足len(序列)>=k)
代码如下:
from random import *
test=[1,23,3,22,13]
print(sample(test,3))
3.shuffle(x[,random])
这个函数的目的就是随机排序,在原序列的基础上进行排序
代码如下:
from random import *
test=[1,23,3,22,13]
shuffle(test)
print(test)

Original: https://blog.csdn.net/apple_51931783/article/details/123144689
Author: 酷尔。
Title: 【Python 基础教程】Python生成随机数
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/285993/
转载文章受原作者版权保护。转载请注明原作者出处!