質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Appiaries

Appiariesはプッシュ通知、データベース、ユーザ認証といったサーバ実装が必要な機能を提供するクラウドサービス・BaaS(Backend as a Service)です。その他にも、シーケンス機能、ロケーション機能、ファイル機能があり、Android/iOSのSDKに対応しています。

Pythonista

Pythonistaは、iOS上でPythonプログラミングができる開発アプリです。さらに、Pythonの関数・変数などを自動で補完する便利なコードエディタや、PythonスクリプトをiOS上で多様な形で機能させる各種機能も内包しています。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

486閲覧

how can i fix AttributeError

islam

総合スコア2

Appiaries

Appiariesはプッシュ通知、データベース、ユーザ認証といったサーバ実装が必要な機能を提供するクラウドサービス・BaaS(Backend as a Service)です。その他にも、シーケンス機能、ロケーション機能、ファイル機能があり、Android/iOSのSDKに対応しています。

Pythonista

Pythonistaは、iOS上でPythonプログラミングができる開発アプリです。さらに、Pythonの関数・変数などを自動で補完する便利なコードエディタや、PythonスクリプトをiOS上で多様な形で機能させる各種機能も内包しています。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/07/29 07:59

i want fix AttributeError
i wrote simple code but the output was wrong attribute error how can i fix
##############
import random
r = random.randrange (-5,11)
print (r)

the output
PS D: \ python example> & C: /Users/ny/AppData/Local/Programs/Python/Python310/python.exe "d: /python example/random.py"
Traceback (most recent call last):
File "d: \ python example \ random.py", line 1, in <module>
import random
File "d: \ python example \ random.py", line 2, in <module>
r = random.randrange (-5, 11)
AttributeError: partially initialized module'random' has no attribute'randrange' (most likely due to a circular import)


i change the file name but it's still excist PS D: \ python example> & C: /Users/ny/AppData/Local/Programs/Python/Python310/python.exe "d: /python example / simple.py" Traceback ( most recent call last): File "d: \ python example \ simple.py ", line 1, in <module> import random ModuleNotFoundError: No module named'random'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Rename the file "random.py" to something like "myrandom.py".

The reason for the error is that you are importing the same file name as the built-in module.

投稿2022/07/29 08:50

編集2022/07/29 22:30
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

islam

2022/07/29 21:25

but i change the name to (simple,py) and the problem still excist i change the file name but it's still excist PS D: \ python example> & C: /Users/ny/AppData/Local/Programs/Python/Python310/python.exe "d: /python example / simple.py" Traceback ( most recent call last): File "d: \ python example \ simple.py", line 1, in <module> import random ModuleNotFoundError: No module named'random'
退会済みユーザー

退会済みユーザー

2022/07/29 22:29 編集

To see which files are imported, try the following: 1. Write and execute the code below: import random print(random.__file__) ↓ 2. Tell me the result and output.
islam

2022/07/30 03:33

it work the result PS D:\python example> & C:/Users/ny/AppData/Local/Programs/Python/Python310/python.exe "d:/python example/simple.py" C:\Users\ny\AppData\Local\Programs\Python\Python310\lib\random.py PS D:\python example> after that i wrote my code which is import random r = random.randrange (-5,11) print (r) it work but why that happen the random medule part of python no need to setup like the other medule thanks any way
islam

2022/07/30 03:40

thanks for your help but i have qustion from your exeprince how do i know the right medule for my project becuse i'm trying to read Python Module Index ( https://docs.python.org/3/py-modindex.html ) but ther's alot of medules which make confusing there's alot of information and i need to know how can i choose the rihgt medule is there any way to know that
退会済みユーザー

退会済みユーザー

2022/07/30 04:27 編集

Too many typos? How to import a library by specifying its absolute path: (Valid in python 3.5 or later) ``` import importlib.util MODULE_PATH = r"C:\Users\ny\AppData\Local\Programs\Python\Python310\lib\random.py" MODULE_NAME = "random" spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH) random = importlib.util.module_from_spec(spec) spec.loader.exec_module(random) r = random.randrange(-5,11) print(r) ``` --- However, this approach is not used in normal development. Just avoid using file names that might duplicate those of built-in modules. It is more efficient to correct errors individually when they occur than to worry about duplicate file names and take the above approach.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問