简体   繁体   中英

How do I make a Python script along with its modules self-contained?

programming novice here with a development management question.

I'm writing a very simple Python script in Windows 7 that will be copied into a directory of Excel files sort it all by filename, exclude the scriptfile in the directory, and process them in order.

I've installed both Python 2.7 and 3.4 on my system which resides in C:\\Python27 and C:\\Python34 respectively.

I've written the sorting part which I'm happy with, but I've had to install the natsort module, which was supposed to be compatible only with 2.7, but was automatically installed by pip into my C:\\Python34... directories instead... I must run the script as "C:\\Python34\\python tvcrunch.py" without getting an ImportError: No module named natsort.

Now I need to install the xlrd and xlwt packages for processing my Excel files, and they installed into C:\\Python27... directories despite being compatible with Python 3.2+.

I think by now you all understand the conundrum I'm in. I would like this script to be portable enough so I can drop it onto any computer and have it run without needing to reinstall all these modules and packages. How do I do that? Script below:

import os
from os import listdir
import natsort

somedir = r'.'
fileList = []

def list_files(path):
    for item in os.listdir(path):
        if os.path.isfile(os.path.join(path, item)):
            if item != r'tvcrunch.py':
                fileList.append(item)
    return fileList

list_files(somedir)
natsort.natsorted(fileList, key=lambda y: y.lower())
print(fileList)

If you are looking to building a single native windows executable check

http://www.py2exe.org/

Tutorial link: http://www.py2exe.org/index.cgi/Tutorial

Support for Python 3.x https://pypi.python.org/pypi/py2exe/0.9.2.0

" py2exe is a Python extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.Spice "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM