![](/img/trans.png)
[英]Import error with virtualenv Python but iPython works fine
[英]Import error in Python 3 but works with Python 2
我想使用Python v3.5.2,但我的筆記本電腦還安裝了Python 2.7.10(這是MacBook)。 我有一個簡單的Python項目結構,如下所示。 注意,可能會顯示一些工件,因為我正在使用IntelliJ作為IDE(例如* .pyc文件和* .iml文件)。
. ├── io │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ └── __init__.cpython-35.pyc │ └── me │ ├── __init__.py │ ├── __init__.pyc │ └── model │ ├── __init__.py │ ├── __init__.pyc │ ├── car.py │ └── car.pyc ├── start.py └── test-python.iml
我的start.py
腳本如下所示。
from io.me.model.car import Car
car = Car("honda", "civic", 2005)
print(car.model)
在終端中,如果我輸入python3 start.py
則會收到以下錯誤。
Traceback (most recent call last): File "start.py", line 1, in from io.me.model.car import Car ImportError: No module named 'io.me'; 'io' is not a package
但是,我決定輸入python start.py
,實際上得到了輸出: civic
。
對我在這里做錯的任何想法嗎?
此外,是否有關於Python項目結構的指南? 來自Java世界,我想知道對於Python項目的結構(例如,像典型的Java Maven項目)是否有推薦的最佳實踐或高度推薦的方法。
python中有一個名為io的內置模塊。 還要在文件夾io所在的目錄中添加__init__.py
。
Python2中的輸出
>>>
>>> import io
>>>
>>>
>>> dir(io)
['BlockingIOError', 'BufferedIOBase', 'BufferedRWPair', 'BufferedRandom', 'BufferedReader', 'BufferedWriter', 'BytesIO', 'DEFAULT_BUFFER_SIZE', 'FileIO', 'IOBase', 'IncrementalNewlineDecoder', 'OpenWrapper', 'RawIOBase', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'StringIO', 'TextIOBase', 'TextIOWrapper', 'UnsupportedOperation', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_io', 'abc', 'open']
>>>
Python3中的輸出
Python 3.4.5 (default, Oct 10 2016, 14:41:48)
[GCC 5.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>>
>>> dir(io)
['BlockingIOError', 'BufferedIOBase', 'BufferedRWPair', 'BufferedRandom', 'BufferedReader', 'BufferedWriter', 'BytesIO', 'DEFAULT_BUFFER_SIZE', 'FileIO', 'IOBase', 'IncrementalNewlineDecoder', 'OpenWrapper', 'RawIOBase', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'StringIO', 'TextIOBase', 'TextIOWrapper', 'UnsupportedOperation', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_io', 'abc', 'open']
>>>
將您的io
包重命名為其他名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.