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
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
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
> 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
On Fri, Oct 22, 2021 at 10:36 PM 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? > 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
New assignmens ...
Hi! Why doesn't this work if (self.ctr:=self.ctr-1)<=0: while this works if (ctr:=ctr-1)<=0: Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On 2021-10-22, Stefan Ram wrote: > Paulo da Silva writes: >>Why doesn't this work >> if (self.ctr:=self.ctr-1)<=0: >>while this works >> if (ctr:=ctr-1)<=0: > > assignment_expression ::= [identifier ":="] expression, > but the attribute references "self.ctr" is no identifier! This seems a surprising omission. You'd expect at least 'attributeref' and 'subscription' to be allowed, if not the whole of 'target'. -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On Sat, Oct 23, 2021 at 6:24 AM Jon Ribbens via Python-list wrote: > > On 2021-10-22, Stefan Ram wrote: > > Paulo da Silva writes: > >>Why doesn't this work > >> if (self.ctr:=self.ctr-1)<=0: > >>while this works > >> if (ctr:=ctr-1)<=0: > > > > assignment_expression ::= [identifier ":="] expression, > > but the attribute references "self.ctr" is no identifier! > > This seems a surprising omission. You'd expect at least 'attributeref' > and 'subscription' to be allowed, if not the whole of 'target'. That's not the primary use-case for assignment expressions, and they were highly controversial. It is much easier to expand it afterwards than to restrict it, or to have the feature rejected because people are scared of some small aspect of it. If you want to propose relaxing the restrictions, make your use-case and attempt to convince people of the value. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On 23/10/2021 08.34, Chris Angelico wrote: > On Sat, Oct 23, 2021 at 6:24 AM Jon Ribbens via Python-list > wrote: >> >> On 2021-10-22, Stefan Ram wrote: >>> Paulo da Silva writes: Why doesn't this work if (self.ctr:=self.ctr-1)<=0: while this works if (ctr:=ctr-1)<=0: >>> >>> assignment_expression ::= [identifier ":="] expression, >>> but the attribute references "self.ctr" is no identifier! >> >> This seems a surprising omission. You'd expect at least 'attributeref' >> and 'subscription' to be allowed, if not the whole of 'target'. > > That's not the primary use-case for assignment expressions, and they > were highly controversial. It is much easier to expand it afterwards > than to restrict it, or to have the feature rejected because people > are scared of some small aspect of it. ie neither can one use subscripted elements, eg list-elements, as the LHS of an assignment expression. -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
With apologies for pressing Send too early... On 23/10/2021 08.41, dn via Python-list wrote: > On 23/10/2021 08.34, Chris Angelico wrote: >> On Sat, Oct 23, 2021 at 6:24 AM Jon Ribbens via Python-list >> wrote: >>> >>> On 2021-10-22, Stefan Ram wrote: Paulo da Silva writes: > Why doesn't this work > if (self.ctr:=self.ctr-1)<=0: > while this works > if (ctr:=ctr-1)<=0: assignment_expression ::= [identifier ":="] expression, but the attribute references "self.ctr" is no identifier! >>> >>> This seems a surprising omission. You'd expect at least 'attributeref' >>> and 'subscription' to be allowed, if not the whole of 'target'. >> >> That's not the primary use-case for assignment expressions, and they >> were highly controversial. It is much easier to expand it afterwards >> than to restrict it, or to have the feature rejected because people >> are scared of some small aspect of it. > > > ie neither can one use subscripted elements, eg list-elements, as the > LHS of an assignment expression. Whereas, creating a list (or tuple...) is legal because the structure's name is an "identifier"! if ( l := [ 1, 2, 3 ] > [ 1, 2 ] ): print( "True" ) -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On 23/10/21 8:49 am, dn wrote: Whereas, creating a list (or tuple...) is legal because the structure's name is an "identifier"! No, the restriction only applies to the LHS. The list construction is on the RHS. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
Às 20:34 de 22/10/21, Chris Angelico escreveu: > On Sat, Oct 23, 2021 at 6:24 AM Jon Ribbens via Python-list > wrote: >> >> On 2021-10-22, Stefan Ram wrote: >>> Paulo da Silva writes: Why doesn't this work if (self.ctr:=self.ctr-1)<=0: while this works if (ctr:=ctr-1)<=0: >>> >>> assignment_expression ::= [identifier ":="] expression, >>> but the attribute references "self.ctr" is no identifier! >> >> This seems a surprising omission. You'd expect at least 'attributeref' >> and 'subscription' to be allowed, if not the whole of 'target'. > > That's not the primary use-case for assignment expressions, and they > were highly controversial. It is much easier to expand it afterwards > than to restrict it, or to have the feature rejected because people > are scared of some small aspect of it. > > If you want to propose relaxing the restrictions, make your use-case > and attempt to convince people of the value. > Well, I didn't follow the discussion of this new feature, but the reason I can see behind allowing it seems so valid for for ctr:=ctr-1 as for self.ctr:=self.ctr-1. The kind of use is exactly the same. One is for a normal function, the other for a method. IMHO this makes no sense at all. Arguable may be for example LHS ctrs[i], or something like that. But self.ctr ...! Too weird. Thanks Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On Sat, Oct 23, 2021 at 12:24 PM Paulo da Silva wrote: > > Às 20:34 de 22/10/21, Chris Angelico escreveu: > > On Sat, Oct 23, 2021 at 6:24 AM Jon Ribbens via Python-list > > wrote: > >> > >> On 2021-10-22, Stefan Ram wrote: > >>> Paulo da Silva writes: > Why doesn't this work > if (self.ctr:=self.ctr-1)<=0: > while this works > if (ctr:=ctr-1)<=0: > >>> > >>> assignment_expression ::= [identifier ":="] expression, > >>> but the attribute references "self.ctr" is no identifier! > >> > >> This seems a surprising omission. You'd expect at least 'attributeref' > >> and 'subscription' to be allowed, if not the whole of 'target'. > > > > That's not the primary use-case for assignment expressions, and they > > were highly controversial. It is much easier to expand it afterwards > > than to restrict it, or to have the feature rejected because people > > are scared of some small aspect of it. > > > > If you want to propose relaxing the restrictions, make your use-case > > and attempt to convince people of the value. > > > Well, I didn't follow the discussion of this new feature, but the reason > I can see behind allowing it seems so valid for for ctr:=ctr-1 as for > self.ctr:=self.ctr-1. The kind of use is exactly the same. One is for a > normal function, the other for a method. > IMHO this makes no sense at all. Arguable may be for example LHS > ctrs[i], or something like that. But self.ctr ...! Too weird. > I've never used ctr:=ctr-1 either, though, so I don't know the actual use cases. Why is this being used in an assignment expression? Is it an ersatz loop? Common use-cases include: if m := re.match(...): while data := thing.read(): etc. All of them are doing exactly two things: testing if something is empty, and if it isn't, using it in a block of code. In what situations do you need to mutate an attribute and also test it, and how much hassle is it to simply break it out into two lines? The onus is on you to show that it needs to be more flexible. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: New assignmens ...
On 23/10/2021 12.51, Greg Ewing wrote: > On 23/10/21 8:49 am, dn wrote: >> Whereas, creating a list (or tuple...) is legal because the structure's >> name is an "identifier"! > > No, the restriction only applies to the LHS. The list construction > is on the RHS. That contention (above) may have been taken slightly out-of-context. Referring back to the previous contribution: mutating a list-element is not legal, because just like an object's attribute addressed with dotted-notation, a subscripted object is not regarded as an identifier. Whereas creating a (whole) new list (for example) IS legal, because the list's name IS an identifier. Yes, the LHS must be an identifier - the point that was made in the first response (quoting "Python Language Reference") and since. -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
