287 lines
6.5 KiB
Python
287 lines
6.5 KiB
Python
import os
|
|
import sys
|
|
import subprocess
|
|
|
|
# set the name of the virtual environment
|
|
# if you want to find the venv from another folder or subfolder:
|
|
# go to Settings "ctrl + ," and set "Python: Venv Path (not synched)" to the path where the venv lives
|
|
venv_name = ".venv"
|
|
|
|
def get_list_of_packages_to_load():
|
|
# define the packages that should be installed
|
|
packages = []
|
|
packages.append('ipykernel')
|
|
packages.append('ipympl')
|
|
packages.append('numpy')
|
|
packages.append('pandas')
|
|
packages.append('pyarrow')
|
|
packages.append('fsspec')
|
|
packages.append('vobject')
|
|
packages.append('requests')
|
|
# packages.append('openpyxl')
|
|
# packages.append('matplotlib')
|
|
# packages.append('pyqt5')
|
|
# packages.append('tirex-ts')
|
|
# packages.append('torch')
|
|
# packages.append('nbconvert')
|
|
# packages.append('pyinstaller')
|
|
# packages.append('oracledb')
|
|
# packages.append('sqlalchemy')
|
|
# packages.append('requests_negotiate_sspi ')
|
|
# packages.append('ldap3')
|
|
# packages.append('mplcursors')
|
|
# packages.append('scipy')
|
|
# packages.append('geopandas')
|
|
# packages.append('geovoronoi')
|
|
# packages.append('ConfigParser')
|
|
|
|
# packages.append('tensorflow') # tensorflow for machine learing
|
|
|
|
print(packages)
|
|
return packages
|
|
|
|
# region functions
|
|
|
|
def create_venv(venv_name):
|
|
# Check if the venv already exists and if not create it
|
|
if os.path.isdir(venv_name):
|
|
print(f'Venv "{venv_name}" found')
|
|
else:
|
|
subprocess.check_call([sys.executable, '-m', 'venv', venv_name])
|
|
print(f'Venv "{venv_name}" created')
|
|
|
|
def get_path_of_venv_executable(venv_name):
|
|
# Get the path to the virtual environment
|
|
venv_path = os.path.join(os.getcwd(), venv_name)
|
|
|
|
# Determine the correct pip executable path based on the OS
|
|
if sys.platform == "win32":
|
|
pip_executable = os.path.join(venv_path, 'Scripts', 'pip.exe')
|
|
else:
|
|
pip_executable = os.path.join(venv_path, 'bin', 'pip')
|
|
|
|
return pip_executable
|
|
|
|
def install_packages(pip_executable, packages_to_load):
|
|
# Function that installs all packages whose names are listed in the packages list
|
|
list_of_failed_packages = []
|
|
|
|
for package in packages_to_load:
|
|
try:
|
|
subprocess.check_call([pip_executable, 'install', package])
|
|
except subprocess.CalledProcessError:
|
|
list_of_failed_packages.append(package)
|
|
|
|
# Some prints for logging
|
|
print("###############################")
|
|
print("The following packages failed to install:")
|
|
for failed_package in list_of_failed_packages:
|
|
print(f"\t\t{failed_package}")
|
|
|
|
def initialize_gitignore(venv_name=".venv"):
|
|
|
|
STANDARD_GITIGNORE = r"""
|
|
############################################
|
|
# project specific gitignore entries
|
|
|
|
############################################
|
|
############################################
|
|
# standard gitignore entries
|
|
############################################
|
|
# Byte-compiled / optimized / DLL files
|
|
__pycache__/
|
|
*.py[codz]
|
|
*$py.class
|
|
|
|
# C extensions
|
|
*.so
|
|
|
|
# Distribution / packaging
|
|
.Python
|
|
build/
|
|
develop-eggs/
|
|
dist/
|
|
downloads/
|
|
eggs/
|
|
.eggs/
|
|
lib/
|
|
lib64/
|
|
parts/
|
|
sdist/
|
|
var/
|
|
wheels/
|
|
share/python-wheels/
|
|
*.egg-info/
|
|
.installed.cfg
|
|
*.egg
|
|
MANIFEST
|
|
|
|
# PyInstaller
|
|
*.manifest
|
|
*.spec
|
|
|
|
# Installer logs
|
|
pip-log.txt
|
|
pip-delete-this-directory.txt
|
|
|
|
# Unit test / coverage reports
|
|
htmlcov/
|
|
.tox/
|
|
.nox/
|
|
.coverage
|
|
.coverage.*
|
|
.cache
|
|
nosetests.xml
|
|
coverage.xml
|
|
*.cover
|
|
*.py.cover
|
|
.hypothesis/
|
|
.pytest_cache/
|
|
cover/
|
|
|
|
# Translations
|
|
*.mo
|
|
*.pot
|
|
|
|
# Django stuff:
|
|
*.log
|
|
local_settings.py
|
|
db.sqlite3
|
|
db.sqlite3-journal
|
|
|
|
# Flask stuff:
|
|
instance/
|
|
.webassets-cache
|
|
|
|
# Scrapy stuff:
|
|
.scrapy
|
|
|
|
# Sphinx documentation
|
|
docs/_build/
|
|
|
|
# PyBuilder
|
|
.pybuilder/
|
|
target/
|
|
|
|
# Jupyter Notebook
|
|
.ipynb_checkpoints
|
|
|
|
# IPython
|
|
profile_default/
|
|
ipython_config.py
|
|
|
|
# pyenv
|
|
# .python-version
|
|
|
|
# pipenv
|
|
#Pipfile.lock
|
|
|
|
# UV
|
|
#uv.lock
|
|
|
|
# poetry
|
|
#poetry.lock
|
|
#poetry.toml
|
|
|
|
# pdm
|
|
#pdm.lock
|
|
#pdm.toml
|
|
.pdm-python
|
|
.pdm-build/
|
|
|
|
# pixi
|
|
.pixi
|
|
|
|
# PEP 582
|
|
__pypackages__/
|
|
|
|
# Celery stuff
|
|
celerybeat-schedule
|
|
celerybeat.pid
|
|
|
|
# SageMath parsed files
|
|
*.sage.py
|
|
|
|
# Environments
|
|
.env
|
|
.envrc
|
|
.venv
|
|
env/
|
|
venv/
|
|
ENV/
|
|
env.bak/
|
|
venv.bak/
|
|
|
|
# Spyder project settings
|
|
.spyderproject
|
|
.spyproject
|
|
|
|
# Rope project settings
|
|
.ropeproject
|
|
|
|
# mkdocs documentation
|
|
/site
|
|
|
|
# mypy
|
|
.mypy_cache/
|
|
.dmypy.json
|
|
dmypy.json
|
|
|
|
# Pyre type checker
|
|
.pyre/
|
|
|
|
# pytype static type analyzer
|
|
.pytype/
|
|
|
|
# Cython debug symbols
|
|
cython_debug/
|
|
|
|
# Abstra
|
|
.abstra/
|
|
|
|
# Ruff
|
|
.ruff_cache/
|
|
|
|
# PyPI config
|
|
.pypirc
|
|
|
|
# Cursor
|
|
.cursorignore
|
|
.cursorindexingignore
|
|
|
|
# Marimo
|
|
marimo/_static/
|
|
marimo/_lsp/
|
|
__marimo__/
|
|
""".strip()
|
|
|
|
project_section_start = (
|
|
"############################################\n"
|
|
"# project specific gitignore entries\n"
|
|
f"{venv_name}\n\n"
|
|
"############################################"
|
|
)
|
|
|
|
# If .gitignore does not exist create full template
|
|
if not os.path.exists(".gitignore"):
|
|
with open(".gitignore", "w") as f:
|
|
f.write(
|
|
project_section_start
|
|
+ STANDARD_GITIGNORE.split("############################################\n############################################")[0]
|
|
)
|
|
return
|
|
|
|
# Create the virtual environment with the given name (default = .venv)
|
|
create_venv(venv_name)
|
|
|
|
# Get the path to the pip executable of the venv
|
|
pip_executable = get_path_of_venv_executable(venv_name)
|
|
|
|
# Load the list of packages that should be loaded into the venv
|
|
packages = get_list_of_packages_to_load()
|
|
|
|
# Install the packages
|
|
install_packages(pip_executable, packages_to_load=packages)
|
|
|
|
# Make sure none of the contents of the venv folder are tracked in git
|
|
initialize_gitignore() |