preload some libraries and scripts in python

后端 未结 3 455
悲哀的现实
悲哀的现实 2021-01-18 18:46

How to preload some libraries and scripts in python before I call python command? Is there something like .bashrc file to deal with predefining some functions/variables befo

相关标签:
3条回答
  • 2021-01-18 18:58

    Create a file, 'my_imports.py' and add the import lines for all your modules.

    import math
    import anothermodule
    import anotherothermodule
    

    Then set an environment variable 'PYTHONSTARTUP' and set it to '/path/to/my_imports.py'.

    Now when you run python at the command prompt, it will load your modules first.

    0 讨论(0)
  • 2021-01-18 19:04

    i wanted to sometimes run python with a bunch of statistics/math stuff loaded (numpy, matplotlib, etc), but othertimes just a simple python without having the overhead of loading modules i wasn't going to use.

    i use ubuntu linux, so i created a python script python-preload.py with the following:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    

    and added an alias to my ~/.bashrc:

    alias pym='PYTHONSTARTUP=/home/$USER/path/to/script/python-preload.py python'
    

    so when i want normal python i run python, and when i want all the math stuff i run pym.

    hope this helps. based off Tony Blundell's answer.

    0 讨论(0)
  • 2021-01-18 19:11

    How about this:

    python -i -c "import math"
    

    And you can put this into a bash file, like b.sh

    #! /bin/bash
    
    python -i -c "import math"
    

    Then you can set whatever you want.

    0 讨论(0)
提交回复
热议问题