You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generate a Python virtual environment project template compatible with vscode
#!/usr/bin/env python# This script generate a Python virtual environment project template compatible with vscode# Author: Dame Diongue# License: public domainimportos, sys, argparse, json, subprocess, shlexfromshutilimportwhichdefparsed_arguments():
# Create commandline parserparser=argparse.ArgumentParser(description='Generate a Python project template compatible with vscode')
# Specify the project nameparser.add_argument('project_name', metavar='project_name')
# Optional argument: set the virtual enviroment parser.add_argument('-e', '--virtualenv', action='store_true', default=False, help='set the virtual enviroment')
parser.add_argument('-c', '--vscode', action='store_true', default=False, help='make necessary setting for vscode')
# Optional argument: set some preinstalled packageparser.add_argument('-p', '--packages', nargs='+', help='set preinstalled packages', metavar='package(s)')
# Let's return all parsed argumentsreturnparser.parse_args()
defsetup_virtualenv(vscode=False):
ifvscode:
os.mkdir('.vscode', mode=0o775)
settings= {
"python.pythonPath": "${workspaceFolder}/venv/bin/python",
"python.venvPath": "${workspaceFolder}/venv"
}
# Create vscode settingwithopen('.vscode/settings.json', 'w') assettings_file:
json.dump(settings, settings_file, indent=2)
# Use python venv if virtualenv is not installed in the systemifwhich('virtualenv') isNone:
execute_command("python -m venv venv")
else:
execute_command("virtualenv venv")
defexecute_command(command):
# Template for executing commandcommand_line=shlex.split(command)
output=subprocess.Popen(command_line, stdout=subprocess.PIPE)
output.communicate()
defmain(argv):
# Get the command line argumentsargs=parsed_arguments()
# Create the project folder and enter to itos.mkdir(args.project_name, mode=0o755)
os.chdir(args.project_name)
# Setup virtual environmentifargs.virtualenv:
setup_virtualenv(args.vscode)
# Check preinstalled packagesifargs.packagesandlen(args.packages) >=1:
file_content=""forpkginargs.packages:
file_content+=pkg+'\n'# Write required packages on filewithopen('requirements.txt', 'w+') aspackage_file:
package_file.write(file_content)
# Temporary activate virtual environment and install packagesos.system('source venv/bin/activate; pip install -r requirements.txt')
if__name__=="__main__":
main(sys.argv)
Usage
usage: pyproject.py [-h] [-e] [-c] [-p packages) [package(s ...]] project_name
Generate a Python project template compatible with vscode
positional arguments:
project_name
optional arguments:
-h, --help show this help message and exit
-e, --virtualenv set the virtual enviroment
-c, --vscode make necessary setting for vscode
-p package(s) [package(s) ...], --packages package(s) [package(s) ...]
set preinstalled packages