Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
¿Cómo importo un archivo python a través de una ruta?

Estoy tratando de hacer que cuando esto se ejecute, ejecute la versión más nueva de la lista.

Aquí está el directorio que quiero:

 ClockOS: bootLoader.py (the file I am starting at) Versions: Version 0.1.1: main.py (Ignore because tis an older version) Version 0.1.2: main.py (The one I want to run/import)

Intenté cargar a través de os.dir y usar sys.path y agregar un __init__.py vacío. ¿Alguien sabe cómo hacer que esto funcione? Mi código:

 import os import re import sys versionList = [] for filename in os.listdir('Versions'): if filename.startswith('Version'): versionList.append(filename) print(f"Loaded {filename}") def major_minor_micro(version): major, minor, micro = re.search('(\d+)\.(\d+)\.(\d+)', version).groups() return int(major), int(minor), int(micro) latest = str(max(versionList, key=major_minor_micro)) print("Newest Version: ", latest) os.path.dirname('Versions/'+str(latest)) sys.path.insert(1, latest) print(sys.path) import main

Por último, necesito que esto funcione tanto en Windows como en Linux, si es posible.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Necesitas usar importlib para hacer lo que quieras. Aquí hay un ejemplo de cómo importar my_function desde un archivo:

 import importlib spec = importlib.util.spec_from_file_location('some_module_name', filename) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) mod.my_function(1)

Probablemente la pregunta sea un duplicado de Importar archivo fuente de python arbitrario. (Python 3.3+)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!