Mailing List Archive

importing a module from a file without the '.py' suffix
I have a file with python code but the name doesn't end with the '.py'
suffix.

What is the easiest way to import this code as a module without changing
its name?

--
Antoon Pardon.
--
https://mail.python.org/mailman/listinfo/python-list
Re: importing a module from a file without the '.py' suffix [ In reply to ]
As far as I know, it can't be done.
If I was REALLY desperate I might try (tested)

import os
os.rename('myfile.myext', 'myfile.py')
import myfile
os.rename('myfile.py', 'myfile.myext')
# with appropriate modifications if myfile is not in the current directory

but this is a horrible solution, subject to race conditions, file
permission errors, wrong state after a crash, and probably other problems.
It could be cleaned up a bit with try ... except, but is still not to be
recommended.

Can you just create a copy of your file with a .py extension?
Best wishes
Rob Cliffe

On 22/10/2021 12:19, Antoon Pardon wrote:
> I have a file with python code but the name doesn't end with the '.py'
> suffix.
>
> What is the easiest way to import this code as a module without
> changing its name?
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: importing a module from a file without the '.py' suffix [ In reply to ]
The (newer) risc os port does a whole bunch of stuff in importlib to do this, as it natively doesn’t use extensions but file types.

I’m not sure if you could do something similar without modifying importlib and re-freezing it.

https://github.com/c-jo/cpython/blob/riscos-310/Lib/importlib/_bootstrap_external.py

Might help. Or not :)

Cheers

Chris

Sent from my iPhone

> On 22 Oct 2021, at 13:02, Rob Cliffe via Python-list <python-list@python.org> wrote:
>
> ?As far as I know, it can't be done.
> If I was REALLY desperate I might try (tested)
>
> import os
> os.rename('myfile.myext', 'myfile.py')
> import myfile
> os.rename('myfile.py', 'myfile.myext')
> # with appropriate modifications if myfile is not in the current directory
>
> but this is a horrible solution, subject to race conditions, file permission errors, wrong state after a crash, and probably other problems.
> It could be cleaned up a bit with try ... except, but is still not to be recommended.
>
> Can you just create a copy of your file with a .py extension?
> Best wishes
> Rob Cliffe
>
>> On 22/10/2021 12:19, Antoon Pardon wrote:
>> I have a file with python code but the name doesn't end with the '.py' suffix.
>>
>> What is the easiest way to import this code as a module without changing its name?
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list
Re: importing a module from a file without the '.py' suffix [ In reply to ]
On Fri, Oct 22, 2021 at 10:36 PM Antoon Pardon <antoon.pardon@vub.be> wrote:
>
> I have a file with python code but the name doesn't end with the '.py'
> suffix.
>
> What is the easiest way to import this code as a module without changing
> its name?
>

It should be possible to use importlib for this:

https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list