OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Import a script from a parent's subdir, with a filename and dirname starting with digits

  • Thread starter Thread starter Basj
  • Start date Start date
B

Basj

Guest
I know the usual Python import system, packages, __init__.py, the __import__ function (see In python, how to import filename starts with a number), etc. I know the classic How do I import other Python files? answers.

Disclaimer: I know the following is not a common file-structure, but sometimes you have to quickly deal with legacy code, without refactoring it.

Code:
|
|- 1 UTILS
|     |-  123myscript.py
|- 2 TESTS
|     |-  test.py

When running test.py, how can I import 123myscript.py, without mangling with the sys.path.append?

Is it possible with importlib? I haven't been able to do it with:

Code:
importlib.import_module("../1 UTILS/123myscript.py", package=None)
<p>I know the usual Python import system, packages, <code>__init__.py</code>, the <code>__import__</code> function (see <a href="https://stackoverflow.com/questions/9090079/in-python-how-to-import-filename-starts-with-a-number">In python, how to import filename starts with a number</a>), etc. I know the classic <a href="https://stackoverflow.com/questions/2349991/how-do-i-import-other-python-files">How do I import other Python files?</a> answers.</p>
<p>Disclaimer: I know the following is not a common file-structure, but sometimes you have to quickly deal with legacy code, without refactoring it.</p>
<pre><code>|
|- 1 UTILS
| |- 123myscript.py
|- 2 TESTS
| |- test.py
</code></pre>
<p>When running <code>test.py</code>, how can I import <code>123myscript.py</code>, without mangling with the <code>sys.path.append</code>?</p>
<p>Is it possible with <code>importlib</code>? I haven't been able to do it with:</p>
<pre><code>importlib.import_module("../1 UTILS/123myscript.py", package=None)
</code></pre>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top