English 中文(简体)
自动进口模块何时使用假装?
原标题:automatically import module when run python script?

我想其他人会事先提出这个问题,但我找不到。 我的问题是,我的发言载于我的<编码>。

ip.ex( import matplotlib as mat )
ip.ex( import matplotlib.pyplot as plt )
ip.ex( import numpy as np )
ip.ex( import pupynere as pu )
ip.ex( import g )
ip.ex( import bsite )
ip.ex( import csvf )
ip.ex( import pandas as pa )
ip.ex( import pickle as pk )
ip.ex( import mathex as mathex )
ip.ex( import os as os )
ip.ex( import re as re )
ip.ex( import scipy as sp )
ip.ex( import mpl_toolkits.basemap as bmp )
ip.ex( from mpl_toolkits.basemap import cm )

之后 如果我使用Python牌照,这些模块将在我开始排队时直接装上,但如果我有纸面文字,例如,ba_plot.py用于做一些地块。 我用互动的方式在柏顿打上了这本书,但当时我想把它放在空壳站内,例如:

chaoyue@chaoyue-Aspire-4750:~$  python ba_plot.py

but before this, each time I need to copy at the beginning of ba_plot.py file the following again:

import matplotlib as mat
import matplotlib.pyplot as plt
import numpy as np
import pupynere as pu
import g
import bsite
import csvf
import pandas as pa
import pickle as pk
import mathex as mathex
import os as os
import re as re
import scipy as sp
import mpl_toolkits.basemap as bmp
from mpl_toolkits.basemap import cm

Because otherwise it will complain it cannot find the module. So, is there anyway that I can avoid do this by including some header file at the beginning of my python script while in the header file all these modules are imported? By this way, I need only to add on line at the beginning of my python script.

问题回答

有一种文字依赖外部开端文字才能发挥作用,这种做法是错误的。 进口 你们应如何这样做。

如果您定期进口一套相当大的物品,将其进口集中到档案中(例如:common_imports.py),然后从中进口(,从共同进口到)。 之后,这只是一条线。 不过,我仍然希望看到明确的进口。

(Another note: in import os as os, the as os is entirely superfluous.)

You need to modify site.py. This script is run every time the Python program is run. For me, it lives in /usr/lib/python2.7/site.py

正如克里斯·摩根所说,这是一种可怕的不良做法,我强烈建议你避免这种做法。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签