Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, November 25, 2020

Install virtualenv

# Step 1: Update your repositories
sudo apt-get update
# Step 2: Install pip for Python 3
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo apt install python3-pip
# Step 3: Use pip to install virtualenv
sudo pip3 install virtualenv
# Step 4: Launch your Python 3 virtual environment, here the name of my virtual environment will be env3
virtualenv -p python3 env3
# Step 5: Activate your new Python 3 environment. There are two ways to do this
. env3/bin/activate # or source env3/bin/activate which does exactly the same thing
# you can make sure you are now working with Python 3
python -- version
# this command will show you what is going on: the python executable you are using is now located inside your virtualenv repository
which python
# Step 6: code your stuff
# Step 7: done? leave the virtual environment
deactivate

 

Tuesday, March 3, 2020

Windows install pip

1. Open cmd as administrator
2. Install python
3. curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
4. Then run the following command in the folder where you have downloaded get-pip.py:
python get-pip.py

5. Add pip installation path to environment PATH
6. check
pip help

Wednesday, November 8, 2017

Python locale error: unsupported locale setting

When using pip install, the local error problem occur.
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
Then select en_US.UTF-8 to install

Occurred when install virtualenv.

Sunday, September 17, 2017

Install python packages (opencv) in virtual envirenment



source ${env}/bin/activate
pip install opencv-python
if cannot be installed via pip
copy the globally installed package, e.g., OpenCV

cp -R /usr/local/lib/python2.7/dist-packages/xxx ${env}/lib/python2.7/site-packages/

Sunday, September 10, 2017

Python-vtk module missing in virtual enviroment

installing Mayavi with pip – no module named vtk



I had the same problem when installing mayavi with python installed in pyenv, and I solved this problem by copy directory to /home/myname/.pyenv/versions/2.7.12/lib/python2.7/site-packages, below is my method:
1. install vtk and python-vtk
sudo apt-get install vtk6
sudo apt-get install python-vtk
2. try
python -c "import vtk"
if failed again, go to step 3
3. the import error may caused by the loss of vtk package folder in your current python, so we should find the vtk folder and copy to your current python(most possible the python in pyenv shims directory)’s directory .
the path of the vtk folder in my ubuntu is
/usr/lib/python2.7/dist-packages/vtk
just copy the vtk folder to your current python’s /site-packages, e.g.
cp -r /usr/lib/python2.7/dist-packages/vtk /home/myname/pyenv/versions/2.7.12/lib/python2.7/site-packages
4. try this again
python -c "import vtk"
5. if no import error, just do
python2 -m pip install mayavi

Sunday, March 10, 2013

Python Time Conversion


From http://emilics.com/blog/article/python_time.html

Python Time Conversion

In python, there are four types that are commonly used to manage time. Timestamps, time tuples, datetime objects, and strings. Programmers often have to convert between these types depending on the situation or API. This article will describe all conversion patterns.

Python Time Conversion Table

Table 1 shows the python conversion table. Click on the pattern you want to see.
Table 1. python time conversion table
input \ outputdatetimetime tupletime stampstring
datetimedatetime

time tuple
datetime

time stamp
datetime

string
time tupletime tuple

datetime
time tuple

time stamp
time tuple

string
time stamptime stamp

datetime
time stamp

time tuple
time stamp

string
stringstring

datetime
string

time tuple
string

time stamp

datetime → time tuple

>>> dt = datetime.datetime(2010, 12, 31, 23, 59, 59)
>>> tt = dt.timetuple()
>>> print tt
time.struct_time(tm_year=2010, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, ...)
datetime → time tuple

datetime → time stamp

>>> dt = datetime.datetime(2010, 12, 31, 23, 59, 59)
>>> ts = time.mktime(dt.timetuple())
>>> print ts
1293868799.0
datetime → time stamp

datetime → string

>>> dt = datetime.datetime(2010, 12, 31, 23, 59, 59)
>>> st = dt.strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2010-12-31 23:59:59
datetime → string

time tuple → datetime

>>> tt = (2010, 12, 31, 23, 59, 59, 4, 365, 0)
>>> dt = datetime.datetime(tt[0], tt[1], tt[2], tt[3], tt[4], tt[5])
>>> print dt
2010-12-31 23:59:59
>>>
>>> dt = datetime.datetime(*tt[0:6])  # same with the code above
>>> print dt
2010-12-31 23:59:59
time tuple → datetime

time tuple → time stamp

>>> tt = (2010, 12, 31, 23, 59, 59, 4, 365, 0)
>>> ts = time.mktime(tt)
>>> print ts
1293868799.0
time tuple → time stamp

time tuple → string

>>> tt = (2010, 12, 31, 23, 59, 59, 4, 365, 0)
>>> st = time.strftime('%Y-%m-%d %H:%M:%S', tt)
>>> print st
2010-12-31 23:59:59
time tuple → string

time stamp → datetime

>>> ts = 1293868799.0
>>> dt = datetime.datetime.fromtimestamp(ts)     # for local time
>>> print dt
2010-12-31 23:59:59
>>>
>>> dt = datetime.datetime.utcfromtimestamp(ts)  # for UTC
>>> print dt
2011-01-01 07:59:59
time stamp → datetime

time stamp → time tuple

>>> ts = 1293868799.0
>>> tt = time.localtime(ts)
>>> print tt
time.struct_time(tm_year=2010, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, ...)
>>>
>>> tt = time.gmtime(ts)
>>> print tt
time.struct_time(tm_year=2011, tm_mon=1, tm_mday=1, tm_hour=7, tm_min=59, tm_sec=59, ...)
time stamp → time tuple

time stamp → string

>>> ts = 1293868799.0
>>> st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2010-12-31 23:59:59
>>>
>>> st = datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2011-01-01 07:59:59
time stamp → string

string → datetime

>>> s = '2010-12-31 23:59:59'
>>> dt = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
>>> print dt
2010-12-31 23:59:59
string → datetime

string → time tuple

>>> st = '2010-12-31 23:59:59'
>>> tt = time.strptime(st, '%Y-%m-%d %H:%M:%S')
>>> print tt
time.struct_time(tm_year=2010, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, ...)
string → time tuple

string → time stamp

>>> s = '2010-12-31 23:59:59'
>>> ts = time.mktime(time.strptime(s, '%Y-%m-%d %H:%M:%S'))
>>> print ts
1293868799.0
string → time stamp