random module error when run as .py - python

It does not load the specific module of random when you run it as a .py. But if I load it in the python shell, I have no problems.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import random
print(random.randint(0,9))
I run it... and:
Traceback (most recent call last):
File "random.py", line 4, in <module>
import random
File "C:\Users\root\Pythin\random.py", line 5, in <module>
print(random.randint(0,9))
AttributeError: module 'random' has no attribute 'randint'
And in the python shell I have no problem:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print(random.randint(0,9))
1
I,m running it in Windows PowerShell (Windows10|64bits). Using the command python random.py. Someone gives me a hand? Thank you very much.

Change the file name of your code to "myrandom.py". And then run the program.
Update: Please don't name your Python source code file to standard modules. As it will cause error. Because right now instead of importing the standard library RANDOM of python, the python imports this random.py and which causes the error.
e.g. Do not give your python source code following names etc.:
random.py
time.py
csv.py
etc... ... ... ....

You can try re-naming the file and try again

Never name your files as random.py.

Related

How to import module in pydroid

I'm having problem in importing my created module in pydroid 3.
The process seems very simple in desktop version of python 3 but I tried doing it in Mobile in pydroid 3 application, but it does not work.
The output was no module named ‘mymodule’ found
When I created my module using Def and saved with mymodule.py
def sky(name):
return ("Welcome, " + name)
Error!
Python 3.8.3 (default, May 27 2020, 02:08:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import mymodule
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'mymodule'
I was having the same problem too , then i moved the module to ru.iiec.pydroid3 . Try it and see if it works .
I was also having the same problem but, after searching on the terminal for some time, I got the directory of modules ; )
here's the path:
/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8
Move your file to this directory. I hope it will help you.
I don't know what's going wrong with your module, but try putting it on PyPI (Website) go to the terminal in the app, and type: pip install Mymodule.
Just make sure your module is saved in the same folder first the just type From followed by the name of your module and import your function then you can just print or do what ever you want with the module.
Also you can just import the whole module by importing it.

python import works interactively but not from script

Disclaimer: I'm not a Windows expert, so this may be something idiosyncratic to Windows.
I'm observing a situation where I can import a module in interactive mode but not when requested from a file. Here is foo.py in its entirety:
#!/usr/bin/env python
import pyftdi
When I try to run this in Python 3.6.4, it fails:
E:\>py foo.py
Traceback (most recent call last):
File "foo.py", line 2, in <module>
import pyftdi
ImportError: No module named pyftdi
...but when I import the module interactively, it succeeds:
E:\>py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyftdi
>>>
(FWIW, I don't get an error when repeating this under Mac OS X.)
Any thoughts about what's going on?
The problem is most likely that the version of python is different and has different modules installed. To fix this you can simply do py -3.6 foo.py instead of py foo.py.
Working directory
I had a situation where the script was importing from it's working directory and the shell was importing from the global libraries.

psutil can not be found within a flask project

I have setup a fairly simple flask project. Within this flask project I want to use psutil but its not being found with the import statement.
> $ ./satelite.py
Traceback (most recent call last):
File "./satelite.py", line 2, in <module>
from app import app
File "/home/neil/monitor/satelite/app/__init__.py", line 4, in <module>
from app import views
File "/home/neil/monitor/satelite/app/views.py", line 6, in <module>
import psutil
ImportError: No module named psutil
However when I use python cli it is.
> $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> print psutil.cpu_percent()
15.3
The same can be said for just a straight forward python script being executed separately.
Standard python modules(subprocess, os, etc) are loading fine. I have tried to focus my google-fu on this but I am getting nowhere so would massively appreciate if anyone can point me in the right direction.
Cheers
Does your first line in satelite.py refers to the same python binary as which python in your terminal ?
(This refer to the #! line)
Maybe you are using python3 in your satelite.py file.

Missing module in python

I recently tried installing powerline-vim, but have been running into problems with it.
Every time I open a new window, I see this error:
Error detected while processing function <SNR>9_UpdateWindows..<SNR>9_pyeval:
line 1:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
File "opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 545, in uuid4 import random
File "opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 545, in uuid4 from os import urandom as _urandom
ImportError: cannot import name urandom
When I delete this line from my .vimrc file (effectively disabling powerline-vim) the error goes away.
python from powerline.ext.vim import source_plugin; source_plugin()
The curious thing about this is when I fire up python from the terminal, the imports work just fine.
Python 2.7.3 (default, Nov 17 2012, 19:54:34)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> from os import urandom as _urandom
>>>
The output of which python:
/opt/local/bin/python
I am running OS X 10.8.2.
Thanks!
Check if your problem is related with that: Python: cannot import urandom module (OS X). Check your sys.path value. Make sure you import os module from your installation of Python, not from system Python.
Check the log configure generates when you build Python for clues. Have you specified prefix when building Python?
Edit the code and print os and sys.path just before the error occurs to check if the plugin changed something. If sys.path is modified, often you may find the place where it is by performing import sys; sys.path = tuple(sys.path). It does not crash on sys.path = smth, but crashes on append() and +=. Maybe that would be enough to show the place where path is modified.
The problem was I was executing the wrong version of Python.
sudo port select python python27-apple
Running that line fixed it.
Thanks!

Why is this module not being found when I run the script but is in the interpreter?

I'm trying to import the tkinter module into my script. I have it installed since the interpreter is able to import it with no problems:
C:\Users\Nacht\Dropbox\Scripts>python
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
Imports fine, no problem. But now when I try to run a script that has the line import tkinter....
C:\Users\Nacht\Dropbox\Scripts>t ls
Traceback (most recent call last):
File "C:\Users\Nacht\Dropbox\Scripts\t.py", line 5, in <module>
import tkinter
ImportError: No module named tkinter
where t is the name of the script and ls a command (it is a command-line interface).
How can the interpreter see it but not the script? Thanks.
EDIT:
The sys.path says, for the interpreter:
C:\Python32\lib\site-packages\distribute-0.6.24-py3.2.egg
C:\Python32\lib\site-packages\selenium-2.15.0-py3.2.egg
C:\Windows\system32\python32.zip
C:\Python32\DLLs
C:\Python32\lib
C:\Python32
C:\Python32\lib\site-packages
C:\Python32\lib\site-packages\win32
C:\Python32\lib\site-packages\win32\lib
C:\Python32\lib\site-packages\Pythonwin
and for the script:
C:\Users\Nacht\Dropbox\Scripts
C:\Python27\lib\site-packages\distribute-0.6.24-py2.7.egg
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info
The script appears to be running with Python 2.7 but when you run the interpreter directly, it is using Python 3.2. As mentioned by #DSM, the name of Tkinter was different (perhaps there are other differences?).
May be there is a problem with path. It can't find the tkinter module. Setting up correct path try to import again.

Categories

Resources