Python on Windows different than Raspberry Pi

I’m having a problem with Python on Windows 10. For example:

On Raspberry PI this code works

import time
print(strftime("%H:%M"))

However, on Windows I get a message that strftime is not defined. I have to specifically say

import time
print(time.strftime(%H:%M"))

Is there a setting to fix this?

Thanks

1 Like

However, on Windows I get a message that strftime is not defined. I have to specifically say

import time
print(time.strftime(%H:%M"))

Is there a setting to fix this?

You could try:

from time import strftime
print(strftime("%H:%M"))
1 Like

Thanks - that works, but I’m wondering why Python doesn’t look for the function strftime in any of the currently loaded (imported) modules? the way it does in Raspberry Pi.

1 Like

Thanks - that works, but I’m wondering why Python doesn’t look for the
function strftime in any of the currently loaded (imported) modules? the way
it does in Raspberry Pi.

This seemingly implicit from X import * effect you are seeing is likely
dependent on the development environment you are in rather than the
underlying hardware platform.

How or whence the customisation took effect is overdetermined as multiple
mechanisms could influence it, from a simple PYTHONSTARTUP environment
variable, an IPython startup file (if you are typing your code in IPython) or
any other IDE-specific settings.

1 Like

Hi gldrplt, you stated:

“On Raspberry PI this code works”

import time
print(strftime("%H:%M"))

I doubt that very much. Or at least, if it does work, it is not running
standard Python, and there is no setting to enable this behaviour. It
goes completely against the Python language standard for that to work in
the general case, and I can’t find anything in the RaspberryPi
documentation to suggest that it should work.

I suspect that you have accidentally done from time import * earlier
in your code, or session, and that’s why it works. If not done by you,
possibly in your PYSTARTUP file.

You can try this to test it. Create a file containing exactly those two
lines above, called “mytest.py”, and then run this from the RaspberryPi
shell:

python3 -E -S mytest.py

I expect you will get the same error as you do on Windows.

If you need help getting the RaspberryPi shell, or command line, you can
read this:

Having Python automatically look inside modules when you call a function
might seem like a good time saver now, but when you gain more experience
and are writing more complex scripts, you will soon learn that this is
trouble waiting to bite.

2 Likes

Steven,
You are correct - it doesn’t work on either the raspberry pi or Windows. I must have done “from time import *” earlier. I also understand your point about specifically identifying the module that you want to import the function from.

Also, can you tell me where the PYSTARTUP file is? I googled it, but i haven’t found anything yet.
Thanks

1 Like

I guess @steven.daprano meant PYTHONSTARTUP (not PYSTARTUP). PYTHONSTARTUP is an environment variable; not a filename, but containing a filename if set. It does not have a default value, but if you set it, Python will execute that file upon interactive startup. See also:

1 Like