Python - modules and packages (common modules)

Keywords: Pycharm vim Python IPython

Modular
In Python, a. py file is called a module
Greatly improves code maintainability
You don't have to start from scratch to write code. When a module is written, it can be referenced elsewhere

vim mymod.py
import mymod
mymod.hello()
mymod.world()

package
What if different people write the same module name? In order to avoid module name collision
Suddenly, Python also introduces the method of organizing modules by directory, which is called package

vim hello.py
vim world.py
vim __init__.py 

import mymod
mymod.hello.hello()
mymod.world.world()

Modules in the system
Built in module
os operates on files and directories, cross platform

In[34]: import os   ##Pour into os module
In[35]: print os.name   ##Display system version
posix
In[36]: os.getcwd() ##Show current directory absolute path
Out[36]: '/root/PycharmProjects'
In[38]: os.listdir("/root/PycharmProjects") ##Show files in specified directory
Out[38]: ['.idea', 'day1', 'day2', 'mymod']
In[39]: os.mkdir("hello")   ##Create directory
In[40]: os.listdir("/root/PycharmProjects")
Out[40]: ['.idea', 'day1', 'day2', 'mymod', 'hello']
In[41]: os.rmdir("hello")   ##Delete directory
In[42]: os.listdir("/root/PycharmProjects")
Out[42]: ['.idea', 'day1', 'day2', 'mymod']

In[47]: os.system("ls")     ##Use shell command, but no output
day1
day2
mymod
Out[47]: 0

In[51]: l = os.popen("ls")  ##View and output
In[52]: print l.read()
day1
day2
mymod

os.path yes os A sub module of , It mainly analyzes, creates, tests and
//Some of his operations encapsulate the path operations of different platforms
In[53]: os.path.split("/root/")
Out[53]: ('/root', '')
In[57]: os.path.basename("/etc/passwd")
Out[57]: 'passwd'
In[58]: os.path.dirname("/etc/passwd")
Out[58]: '/etc'

random module

In[62]: import random
In[63]: random.randint(1,100)   ##Randomly generated number
Out[63]: 14
In[64]: random.randint(1,100)
Out[64]: 28
In[65]: random.randint(1,100)
Out[65]: 88

In[68]: random.random() ##Randomly generate decimal between (0, 1)
Out[68]: 0.12270722248027299
In[69]: random.uniform(1,5) ##Randomly generate decimals within the specified range
Out[69]: 2.0753958437636566

In[70]: random.randrange(1,50,2)    ##Generate integers at 2 intervals
Out[70]: 13
In[71]: random.randrange(1,50,2)
Out[71]: 19

In[73]: random.choice(["one", 'two', 'zero'])   ##Random selection
Out[73]: 'one'
In[74]: random.choice(["one", 'two', 'zero'])
Out[74]: 'zero'

In[80]: random.sample(["one", 'two', 'zero'],2) ##Randomly select two
Out[80]: ['one', 'zero']
In[81]: random.sample(["one", 'two', 'zero'],2)
Out[81]: ['zero', 'one']


In[82]: li = [1,2,3,4,5,6,7,8,9]    ##Upset
In[83]: random.shuffle(li)
In[84]: print li
[2, 8, 1, 4, 6, 9, 5, 7, 3]

sys module
It mainly provides system related configuration and operation, encapsulates the interaction of detecting, changing interpreter runtime and resources

In[85]: import sys
In[86]: print sys.path
['/opt/pycharm/helpers/pydev', '/opt/pycharm/helpers/pydev', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/IPython/extensions', '/root/PycharmProjects']

In[89]: sys.path.insert(0,"/root")
In[90]: print sys.path
['/root', '/opt/pycharm/helpers/pydev', '/opt/pycharm/helpers/pydev', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/IPython/extensions', '/root/PycharmProjects', '/root']

Third party module
Networking required
Through setuptools
pip install package name or pycharm

Posted by luke101 on Mon, 04 May 2020 09:22:38 -0700