This is my project structure.
├─demo_data
├─findb
│ └─__pycache__
├─management
└─tw_equity
├─migrations
│ └─__pycache__
└─__pycache__
Now I try to insert demo data to my models.
So I create commands.pyfor the process.
# ./management/commands.py
from datetime import datetime
from tw_equity.models import (
TradingDate,
Basic,
Price,
Holder,
)
with open('demo_data/trading_date.csv') as f:
for row in f:
data = datetime.strptime(row, "%Y-%m-%d").date()
trading_date = TradingDate(
trading_date=data
)
trading_date.save()
print('Insert Successfully.')
But the error keeps showing up.
(Findb) C:\Users\ycyta\python-django-Findb>management\commands.py
Traceback (most recent call last):
File "C:\Users\ycyta\python-django-Findb\management\commands.py", line 2, in <module>
from tw_equity.models import (
ModuleNotFoundError: No module named 'tw_equity'
Already tried
python manage.py shell, it is all good.
(Findb) C:\Users\ycyta\python-django-Findb>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from tw_equity.models import TradingDate
>>> TradingDate
<class 'tw_equity.models.TradingDate'>
Mostly similar questions before were concerned about __ini__.py, but I already have.
Thanks for helping.
Related
I'm teaching myself Python and was just "exploring". Google says that datetime is a global variable but when I try to find todays date in the terminal I receive the NameError in the question title?
mynames-MacBook:pythonhard myname$ python
Enthought Canopy Python 2.7.3 | 64-bit | (default, Aug 8 2013, 05:37:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> date = datetime.date.today()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'datetime' is not defined
>>>
You need to import the module datetime first:
>>> import datetime
After that it works:
>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)
It can also be used as below:
from datetime import datetime
start_date = datetime(2016,3,1)
end_date = datetime(2016,3,10)
I'm following the documentation and using the latest PyMuPDF (1.18.13). However Pixmap.tobytes() isn't working for me:
zoom = 2 # zoom factor
mat = fitz.Matrix(zoom, zoom)
pix = page.getPixmap(matrix = mat)
stream = pix.tobytes(output="png")
AttributeError: 'Pixmap' object has no attribute 'tobytes'
Example of documentation:
What might be the issue here?
I am PyMuPDF's maintainer.
What is your configuration? I just tried your code on Windows and Linux each with v1.18.13 and it work.
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fitz
>>> fitz.version
('1.18.13', '1.18.0', '20210505063222')
>>> doc=fitz.open("v110-changes.pdf")
>>> page=doc[0]
>>> pix=page.get_pixmap()
>>> b=pix.tobytes()
>>>
Windows:
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fitz
>>> fitz.version
('1.18.13', '1.18.0', '20210505063222')
>>> doc=fitz.open("v110-changes.pdf")
>>> page=doc[0]
>>> pix=page.get_pixmap()
>>> b = pix.tobytes()
>>>
My python code shown below is not retrieving any dates before the year 1900. I learned that this is a limitation of datetime.strptime().
I have tried to follow some of the workaround mentioned in similar posts , Is there any way to use a strftime-like function for dates before 1900 in Python? but they seem a bit complicated for me to follow. I also learned that there is a patch available to fix this issue. https://bugs.python.org/file10253/strftime-pre-1900.patch
I tried to install the patch in Pycharm by copying the patch to a text file, but I am getting the following error message. Any ideas on what I need to do to run the patch successfully to get dates before 1900 ?
Pycharm Patch Error Screenshot
My Code:
from datetime import datetime
import csv
with open('train.csv', 'r') as f_input, open('sample.txt', 'w') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
for row in csv_input:
for date_format in ['%Y']:
try:
converted = datetime.strptime(row[3], date_format)
csv_output.writerow([row[0], row[1], row[2], converted.strftime(date_format)])
except ValueError:
pass
I realize this isn't what you asked, but I'll put it out there anyway since I'm assuming that if the answers to the linked question are too complicated to follow, the idea to patch your problem away successfully is probably unrealistic.
The limitation you are seeing is present in Python2:
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
It is somewhat rectified in Python3.2, and fully rectified in Python3.3 and onward:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
'1899-01-01'
Parsing works too:
>>> d = datetime.strptime('0113-01-01','%Y-%m-%d')
>>> d
datetime.datetime(113, 1, 1, 0, 0)
>>> d.isoformat()
'0113-01-01T00:00:00'
>>> d.strftime('%Y-%m-%d')
'113-01-01'
So if that is an acceptable option, you could switch over to Python3.3+ and you won't have this issue.
Is file a keyword in python?
I've seen some code using the keyword file just fine, while others have suggested not to use it and my editor is color coding it as a keyword.
No, file is not a keyword:
>>> import keyword
>>> keyword.iskeyword('file')
False
The name is not present in Python 3. In Python 2, file is a built-in:
>>> import __builtin__, sys
>>> hasattr(__builtin__, 'file')
True
>>> sys.version_info[:2]
(2, 7)
It can be seen as an alias for open(), but it was removed in Python 3, where the new io framework replaced it. Technically, it is the type of object returned by the Python 2 open() function.
file is neither a keyword nor a builtin in Python 3.
>>> import keyword
>>> 'file' in keyword.kwlist
False
>>> import builtins
>>> 'file' in dir(builtins)
False
file is also used as variable example from Python 3 doc.
with open('spam.txt', 'w') as file:
file.write('Spam and eggs!')
As others suggested, type in Python 3 it is not defined by default:
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
The color coding in VS Code and possibly other editors probably refers to Python 2, where it is defined by default, it is the type returned by open():
Python 2.7.18 (default, Jul 1 2022, 12:27:04)
[GCC 9.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> file
<type 'file'>
Python ctype can't find AntTweakBar.dll ?? and the file dosn't exist for python OS file system call, but exists in the c:\Windows\System32 dir.
C:\Users\dkm>dir c:\Windows\System32\ant*dll
Volume in drive C is Windows 7
Volume Serial Number is FC61-5FBB
Directory of c:\Windows\System32
23/07/2012 12:05 AM 641,536 AntTweakBar.dll
22/07/2012 11:43 PM 774,144 AntTweakBar64.dll
2 File(s) 1,415,680 bytes
0 Dir(s) 47,418,232,832 bytes free
C:\Users\dkm>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes.util import find_library
>>> import os
>>> print os.path.exists('C:\\Windows\\System32\\antweakbar.dll')
False
>>> find_library("AntTweakBar")
>>>
why can't ctype find the dll?
there are 2 t's in the file name
antweakbar.dll - name given by you
AntTweakBar.dll - actual file name
C:\\Windows\\System32\\antweakbar.dll
should be C:\\Windows\\System32\\anttweakbar.dll
for example (checking with inetcfg.dll, as i don't have anttweakbar.dll), see below for Python 2.7.2
In [136]: import os
In [137]: os.path.exists("C:\\Windows\\System32\\inetcfg.dll")
Out[137]: True