跳转至

Python安装

1 Linux 下安装

  1. 安装python3:yum install -y python3
  2. 安装pip(当安装python3后没有pip命令时安装):yum install -y pip
  3. 安装ipython:yum install -y ipython

2 安装第三方包

  1. pip3 install 模块名:默认安装
  2. pip install 模块名 -t 安装路径:指定安装位置

3 使用自定义路径下的包

  • 参考:http://c.biancheng.net/view/4645.html

3.1 方法一修改 sys.path 变量

  • 向sys.path变量添加包查询路径
import sys
sys.path.append('./lib')
import fire

def hello(name="World"):
  return "Hello %s!" % name

if __name__ == '__main__':
  fire.Fire(hello)
  • fire包在lib目录下
[shw@localhost python]$ tree -L 2
.
├── lib
│   ├── fire
│   ├── fire-0.4.0-py3.9.egg-info
│   ├── __pycache__
│   ├── six-1.16.0.dist-info
│   ├── six.py
│   ├── termcolor
│   └── termcolor-2.0.1.dist-info
└── test.py

7 directories, 2 files
[shw@localhost python]$ cat test.py 
  • 执行
[shw@localhost python]$ python test.py 
Hello World!

3.2 设置 PYTHONPATH 变量

[shw@localhost python]$ python test.py 
Traceback (most recent call last):
  File "/home/shw/python/test.py", line 3, in <module>
    import fire
ModuleNotFoundError: No module named 'fire'
[shw@localhost python]$ export PYTHONPATH=/home/shw/python/lib
[shw@localhost python]$ python test.py 
Hello World!