venv setup, activate & exit
- setup:
1
$ python3 -m venv
- activate:
1
2$ . venv/Scripts/activate # windows
$ source venv/bin/activate # linux & Mac OS - exit:ref: https://blog.csdn.net/weixin_30292745/article/details/101445788?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_v2~rank_aggregation-3-101445788.pc_agg_rank_aggregation&utm_term=python+%E5%85%B3%E9%97%ADvenv&spm=1000.2123.3001.4430
1
$ deactivate
setup flask development env inside react project
take my project as an example:
- mkdir react-app/api/
- under
api/
, create file namedapi.py
- (venv) pip install python-dotenv
- create
.env
file under /api - write the following content into .env:start server:
1
2FLASK_APP=api.py
FLASK_ENV=developmentref: https://stackoverflow.com/questions/54600434/how-to-set-flask-env-inside-config-file1
$ flask run
UnicodeEncodeError
- ‘charmap’ codec can’t encode characters in position 87-93: character maps to
https://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters
tried multiple ways to print the raw context directly but failed, but it can be written into files with1
2with open('books.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(item, ensure_ascii=False) + '\n')UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u0130’
‘\u0130’ is ‘I’ in Latin, charmap is the default encoding format used locally.
solution:<target-string>.encode('utf-8')
Note, it cannot be fully handled with above encoding function, it actually turn out to be like"\u0130stanbul"
currently I have no better solution but add one more function to translate the Latin letter to English …
pip install & version control under vitural env
- check how many modules are installed in current env:
1
$ pip list
- check where pip installed package is:e.g.
1
$ pip show <module-name>
1
2
3
4
5
6
7
8$ pip show phantomjs
Name: phantomjs
Version: 1.4.1
Summary: Python wrapper for PhantomJS
Location: c:\users\...\appdata\local\programs\python\python37-32\lib\site-packages
Requires:
Required-by:
(venv) - generating
requirements.txt
for all required packages:1
$ pip freeze > requirements.txt
- pip install all required packages in
requirements.txt
:1
$ pip install -r requirements.txt