Python

Table of Contents

Profile

Creating profiles in Python

Python supports user profiles which are user generated code saved into a default file location, that the python interpreter or native IDLE shell loads on startup of the shell and runs the code. To get started using a profile, first run the code below to determine the location that python uses to load up the default profile from.

>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'


Now you can create a file named usercustomize.py in that directory and put anything you want in it. It will affect every invocation of Python, unless it is started with the -s option to disable the automatic import. sitecustomize works in the same way, but is typically created by an administrator of the computer in the global site-packages directory, and is imported before usercustomize. See the documentation of the site module for more details.

First find the directory of current
>>>import site
>>>site.getusersitepackages()
'C:\\Users\\USERACCOUNTNAME\\AppData\\Roaming\\Python\\Python36\\site-packages'


Although the output shows the above file path exists it doesn’t' and needs to be created.

Create the folders down to the subdirectory below using the windows file explorer.
C:\Users\Sumeet\AppData\Roaming\Python\Python35
Then create a file in that directory named usercustomize.py. the os.chdir() method will change the current working directory to the site package folder. And the open() method can be used to create a file.
>>>os.chdir('C:\\Users\Sumeet\Appdata\Roaming\Python\Python36\site-packages')
>>>txt_object = open("usercustomize.p", "w+")


Anything you place in this file will be your startup profile when starting python for the first time through any default shell, IDLE or Interpreter such as PowerShell. Anything with .py extension can be opened with notepad to start typing or you may use the open() method to amend the file from within the shell and add data.

Visit next page to learn about - Theory