windows平台python脚本执行环境搭建笔记

文章介绍了如何下载和配置Python环境,强调了添加到系统路径的重要性。接着,详细讲解了如何使用pip安装扩展模块如matplotlib,并提供了处理超时和选择镜像站的技巧。之后,文章转向Python脚本的编写和执行,展示了一段用于傅里叶变换和滤波直流分量的示例代码,强调了Python在数学和脚本执行上的实用性。
摘要由CSDN通过智能技术生成

1.python脚本环境下载

这里是原始发布源:

https://www.python.org/downloads/release/python-3114/https://www.python.org/downloads/release/python-3114/安装时记得添加进系统path,这样你可以随时调用python环境。

2.扩展模块的安装

step1.找到python安装位置,推荐安装everything.然会直接找python这个目录,它一般的位置在:

step2.打开cmd.exe,切换至这个目录, 执行:

python -m pip install matplotlib  --timeout 200 -i http://mirrors.aliyun.com/pypi/simple/  --trusted-host mirrors.aliyun.com

上面:

  • pip install <module_name>是基础的命令字。
  • timeout参数指定超时时间,单位秒。
  • -i 制定镜像站,国外的源访问速度非常慢。
  • --trusted-host是在指定信任这个私有源

一旦脚本执行时提示:

ModuleNotFoundError: No module named 'matplotlib'

就重复上述安装过程。

3.脚本编写和执行

只要学过一门计算机语言,就可以在ChatGPT的帮助下,熟悉程序结构和语法。

脚本代码存储为.py后缀,就可以直接点击执行。

如果需要排错,进相应的目录内,执行命令行即可。

在python脚本执行环境内直接执行 python命令,可以这样:

D:\>python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 13.0
>>> b = 1/a
>>> print(b)
0.07692307692307693

它起码可以替代计算器。 现在,可能是windows平台上最好用的脚本执行环境。甚至数学部分,可以部分替代matlab.

附录A:一段工作上正在使用的示例代码

我在之前仅仅了解非常有限的python语法,可能只学过不到50小时。在AI的助力下,你能比用搜索器更快地解决问题:

# show how to use FFT, filtered DC signal and return to SampleValue-time type.
# the basic concept is coming from ChatGPT.
# Write in python language.
#
# created by twicave.
# Jun09,2023
#
import numpy as np
import matplotlib.pyplot as plt

# 定义正弦信号
# 产生一个包含1000Hz、2000Hz两个正弦波信号和一个直流分量的复合信号
Fs = 20000;
f1 = 1000;
f2 = 2000;
t = np.arange(0, 0.01, 1/Fs);
signal = np.sin(2*np.pi*f1*t) + 0.7*np.sin(2*np.pi*f2*t) + 800;

fig = plt.figure(1, figsize=(8, 10));
plt1 = fig.add_subplot(2, 1, 1)
plt1.plot(t,signal)
plt1.set_xlabel('Time (s)')
plt1.set_ylabel('Amplitude')
plt1.set_title('Original Signal')


# 下面,将对应信号转至频域消除0点后转回时域
x = signal

# 傅里叶变换
X = np.fft.fft(x)  # 计算原始信号的傅里叶变换

# 去除直流分量
X[0] = 0;

# 幅角变换
phase_shift = np.zeros(len(X))
phase_shift[len(X)//2] = -np.angle(X[len(X)//2])
X_corrected = X * np.exp(1j * phase_shift)

# 转回时域
x_withoutDC = np.real(np.fft.ifft(X_corrected));

print(x_withoutDC);
s_corrected = x_withoutDC;

plt2 = fig.add_subplot(2, 1, 2)
plt2.plot(t, s_corrected.real)
plt2.set_xlabel('Time (s)')
plt2.set_ylabel('Amplitude')
plt2.set_title('After filtered the DC of signal')

fig.suptitle("Demo program : filter DC of signal");
fig.show()

附录A Python语法及模块手册

Python的函数说明是按照版本分别放置的,下面的代码可以查询自己的python版本

#查询python版本:
import sys
print(sys.version)

这里是我在用的版本的某个函数的在线帮助系统链接:

socket — Low-level networking interface — Python 3.11.4 documentationSource code: Lib/socket.py This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms. Availability: not ...https://docs.python.org/3/library/socket.html#socket.socket.recvfrom

很奇怪。python的核心网站上,没有重要的模块,比如numpy的信息,干干静静的,连一个链接都没有。然后这个模块有自己独立的网站和帮助系统:

NumPy user guide — NumPy v1.24 Manual

附录B  最常用的语法功能

#单行注释
#这是一个单行注释


#多行注释
“”“
三个引号开启一组多行注释
这仍然是注释。
“”“

#文件编码
#默认编码是utf8,
#如果你的.py文件无法执行。请另存为.uft8格式。notepad就能干这件事。

#引用模块
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt

#最常用的一窗体多图语法
import matplotlib.pyplot as plt

fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=False, figsize=(6, 9))
ax1.plot(t, sig)
ax1.set_ylabel('Original Signal')
ax2.plot(np.arange(0, N/2)*fs/N, 20*np.log10(np.abs(np.fft.fft(h)[:N//2])))
ax2.set_ylabel('Filter Frequency Response (dB)')
ax3.plot(t, sig_filtered)
ax3.set_ylabel('Filtered Signal')
ax3.set_xlabel('Time (s)')
ax4.plot(t, t);
ax4.set_xlabel('pt = ' + str(len(t)));
plt.show()

#打印任意对象
print(A)

 附录 C 有价值链接 

  1.  python的官方终端客户支持网站:
    Discussions on Python.orgDiscussions related to the Python Programming Language, Python Community, and Python Software Foundation operations.https://discuss.python.org/

  2. 一个跳板,A Guy talking about 语法检查:
    How ruff changed my Python programming habits - Matthias Kestenholzicon-default.png?t=N6B9https://406.ch/writing/how-ruff-changed-my-python-programming-habits/

  3. python3的最新在线帮助:
    Python Documentation contents — Python 3.11.4 documentationWhat’s New in Python- What’s New In Python 3.11- Summary – Release highlights, New Features- PEP 657: Fine-grained error locations in tracebacks, PEP 654: Exception Groups and except*, PEP 678: Exc...https://docs.python.org/3/contents.html

  4. 使用VS2022免费社区版编写编译调试python:
    管理 Python 环境和解释器 - Visual Studio (Windows) | Microsoft Learn使用“Python 环境”窗口管理全局、虚拟和 Conda 环境。 安装 Python 解释器和包,并将环境分配给 Visual Studio 项目。icon-default.png?t=N6B9https://learn.microsoft.com/zh-cn/visualstudio/python/managing-python-environments-in-visual-studio?view=vs-2022Visual Studio documentation | Microsoft LearnLearn how to use Visual Studio to develop applications, services, and tools in the language of your choice, for any platform or device.icon-default.png?t=N6B9https://learn.microsoft.com/en-us/visualstudio/windows/?view=vs-2022&preserve-view=trueVisual Studio - Microsoft Q&AMicrosoft Q&A is the best place to get answers to your technical questions on Microsoft products and services.icon-default.png?t=N6B9https://learn.microsoft.com/en-us/answers/tags/176/vs

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子正

thanks, bro...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值