2015-11-01 77 views
0

我有很多的Python腳本的共享導入文件,他們有着共同的模塊,如下面的示例我可以有很多的Python腳本

我怎麼能解壓縮到像common_includes.py

而且在每個另一個文件Python文件,我只需要做require common_includes

#!usr/bin/python3 
    # -*- coding: utf-8 -*- 
    import requests 
    import itertools 
    from html.parser import HTMLParser 
    from lxml import etree 
    import lxml 
    import lxml.html as html 
    from bs4 import BeautifulSoup 
    import json 
    import requests 
    import os 
    import urllib.parse 
    import urllib.request 
    import urllib.error 
    from re import sub 
    from decimal import Decimal 
    from pdb import set_trace 
    import datetime 
    import pymongo 
    import yaml 
    from concurrent.futures import ThreadPoolExecutor 

回答

0

創建包含所列進口Python文件.py,使文件名common_includes.py (from example)。現在,當你想將文件鏈接到其他文件時,只需將其包含在頂部即可。

import sys 
sys.path.append(r'C:\Users\Python Scripts') # Filepath 
import common_includes # Filename | do not add .py 

你只需要使用sys模塊如果common_includes文件不位於同一目錄中,你正在使用的文件。

現在,在導入時,你可以使用:

import common_includes 
import common_includes as CI 
from common_includes import * 

#Using the modules (os module as example) 
common_includes.os 
CI.os 
os 

要檢查它的聯繫,只是運行在頂部提供的代碼空白文件,並使用其中的一個模塊。

相關問題