Topics

I can't import random module


javierfernandezc@...
 

I would like to run a code like this:

import random
 
def runScript():
    foo = ['a', 'b', 'c', 'd', 'e']
    print(random.choice(foo))  

But I have this error:

"no module named random"


Shush
 

On Wednesday, 30 September 2020, 04:36:18 PM IST, javierfernandezc@... <javierfernandezc@...> wrote:


I would like to run a code like this:

import random
 
def runScript():
    foo = ['a', 'b', 'c', 'd', 'e']
    print(random.choice(foo))  

But I have this error:

"no module named random"


javierfernandezc@...
 

I have already seen that, thanks. I tried that and failed againg:

clr.AddReferenceByName('IronPython.Modules')
from IronPython.Modules import *
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))  

ERROR: No module named random


Alex Earl
 

Do you have the StdLib added to your path? random.py is in the Python stdlib and it imports the _random module from IronPython.Modules. Without the stdlib, you can't import random.


On Wed, Sep 30, 2020 at 7:31 AM <javierfernandezc@...> wrote:
I have already seen that, thanks. I tried that and failed againg:

clr.AddReferenceByName('IronPython.Modules')
from IronPython.Modules import *
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))  

ERROR: No module named random




javierfernandezc@...
 

Thanks so much for your quick response, Alex.

I am using the IronPython nuget package (version 2.7.5) into a .Net project. Sorry in advance for my ignorance, but stdlib should be included within IronPython.dll, isn't it?

Kind regards,
Javier Fernández


Alex Earl
 

Hi Javier,


This is done because not all users need the stdlib and so the stdlib can be modified without doing a new IronPython release.

Regards,

Alex

On Wed, Sep 30, 2020 at 8:08 AM <javierfernandezc@...> wrote:
Thanks so much for your quick response, Alex.

I am using the IronPython nuget package (version 2.7.5) into a .Net project. Sorry in advance for my ignorance, but stdlib should be included within IronPython.dll, isn't it?

Kind regards,
Javier Fernández




javierfernandezc@...
 
Edited

Finally, I have chosen to make my own random choice function instead:

def RandomChoice(list):

    secondsMII = int((datetime.utcnow() - datetime(2000, 1, 1)).total_seconds() * 1000)

    randomIndex = secondsMII % len(list)

    return list[randomIndex]


Markus Schaber
 

Hi,

 

As it’s IronPython, you could also use the .NET Framework random generators. J

 

Mit freundlichen Grüßen / Best regards

Markus Schaber


CODESYS Group
We
software Automation.


CODESYS Development GmbH
A member of the CODESYS Group

Dipl.-Inf.
Markus Schaber | Team Leader Automation Server
Tobias-Dannheimer-Str. 5 | 87439 Kempten | Germany
Tel. +49-831-54031-979
m.schaber@... | codesys.com

CODESYS Store: store.codesys.com
CODESYS Talk: forge.codesys.com
CODESYS Online Help: help.codesys.com


Geschäftsführer / CEOs: Dipl.-Inf. Dieter Hess, Dipl.-Inf. Manfred Werner
Handelsregister / Trade register: Kempten HRB 13379
| USt-IDNr. / Tax ID No.: DE 307355786


Diese E-Mail enthält möglicherweise vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind
oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received
this e-mail in error, please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure
or distribution of the material in this e-mail is strictly forbidden.

Von: users@ironpython.groups.io <users@ironpython.groups.io> Im Auftrag von javierfernandezc@...
Gesendet: Mittwoch, 30. September 2020 18:36
An: users@ironpython.groups.io
Betreff: Re: [ironpython] I can't import random module

 

[Edited Message Follows]

Finally, I have chosen to make my own random choice function instead:

def RandomChoice(list):

    secondsMII = int((datetime.utcnow() - datetime(2000, 1, 1)).total_seconds() * 1000)

    randomIndex = secondsMII % len(list)

    return list[randomIndex]


javierfernandezc@...
 

Thanks Markus, this is the best solution, a simple "from System import...", and use random object.