Start virtual python environment.
pipenv shell
Install requirments from a Pipfile
pipenv install
example project
New project "switch-boat" imported from switch-pi
use pipenv to install requirements from PipFile.
I also had to install python 3.7 with pyenv
pyenv install 3.7.13
I had to uninstall virtualenv since pipenv replaces it.
pip uninstall virtualenv
ip install pipenv
In the switch-boat repo use
pipenv install
Install the new virtualenv fromt he PipFile
pipenv install
Start the virtual env
`pipenv shell
In vscode install the "Python environmetn manager" now select pipenv switch-boat environment terminal
use virtaulenv wrapper
pyenv global 3.7.3
Switch to python version 3 or largersudo pip install virtualenvwrapper
install virtualenv wrappermkvirtualenv env_name
make the python virtual environmentworkon env_name
enter the virtual envpip freeze > requirements.txt
Create a text file of all the python libspip install -r requirements.txt
install from that list
Install with
sudo apt-get install python3-venv
To make a new virtual env use
python3 -m venv tutorial-env
A common directory location for a virtual environment is .venv
To source the environment use
source tutorial-env/bin/activate
install requirements from a requirements.txt
pip install -r requirements.txt
Show the version of a pip installed package. This is useful to add to requirements.txt
pip show django-filter
requirements.txt might look like this
Automatically create a requirments.txt file for a virtualenv
pip freeze > requirements.txt
ipdb==0.8
ipython==1.1.0
mock==1.0.1
newrelic==2.4.0.4
nose==1.3.0
To automatically activate a virtual env when entering a directory containing a .env file use autoenv On OSX (you need to have installed homebrew)
brew install autoenv
On Linux
git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
pip install mock
pip install nose
Nose is a unit tester. Create a test class and test functions for nose to run eg:
class TestExampleTwo:
def test_c(self):
assert 'c' == 'c'
Here, nose will first create an object of type TestExampleTwo, and only then run test_c: Run the tests with,
$ nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.003s
To run a particular and prevent nose from capturing the output run the following
nosetests --nocapture test_file_name.py:test_class_name.test_case_name
Most new test functions you write should look like either of these tests -- a simple test function, or a class containing one or more test functions.
def test():
setup_test()
try:
do_test()
make_test_assertions()
finally:
cleanup_after_test()
easy_install readline
pip install ipython
pip install ipdb
This is a Python debugger. To set a break point with ipdb add the following to your python code:
import ipdb; ipdb.set_trace()
- c continue
- s step into
- n step over
- unt continue until a line below this is reached
- p print the value of an expression
- ! execute the statement
- l list the 10 lines around the current position
An easy solution is to use pyenv
brew update
brew install pyenv
To list availiable python versions to install
pyenv install --list
To install a verion e.g python 3.5
pyenv install 3.5.0
To set a local application-specific Python version.
pyenv local 3.5.0
pyenv rehash
To set the global python version in all shells by writing the version name to the ~/.pyenv/version file. This version can be overridden by an application-specific .python-version file, or by setting the PYENV_VERSION environment variable.
pyenv global 2.7.6
pyenv rehash
List all Python versions known to pyenv, and shows an asterisk next to the currently active version.
pyenv versions
To create a new virtualenv with a pyenv version of python set the local version of python with pyenv then create the virtualenv and use the pyenv version of python.
pyenv local 3.5.0
mkvirtualenv -p `pyenv which python` n_note_py2mk
To use a virtualenv and get jupyter notebook using the python version form the virtualenv activate the virtualenv and then install jupyter within the active virtualenv
(n_note_py2mk)$ pip install jupyter
jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:
(n_note_py2mk)$ pip install ipykernel
Next, set up the kernel
install --user --name n_note_py2mk --display-name "n_note_py2mk"
then start jupyter notebook (the venv need not be activated for this step)
(n_note_py2mk)$ jupyter notebook
or
jupyter notebook
pip install virtualenv
To set up a new Python environment, move into the directory where you would like to store the environment, and use the virtualenv utility to create the new environment.
virtualenv venv
To use an environment, run
source venv/bin/activate
Your command prompt will change to show the active environment. Once you have finished working in the current virtual environment, run deactivate to restore your settings to normal.
A set of command line tools to virtualenv can be installed to make using virtualenv more enjoyable.
pip install virtualenvwrapper
...
export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv env1
Create a virtual environment:
mkvirtualenv venv
Work on a virtual environment:
workon venv
Deactivating is still the same:
deactivate
List all of the environments:
lsvirtualenv
to delete a virtual env. Just deactivate and delete the folder in ~/.virtualenvs/ or
(mynewenv)$ deactivate
rmvirtualenv mynewenv
Conda Enviroments
Check Conda is installed
Update
Create a virtual environment for your project
List conda environements
Activate your virtual environment.
Install additional Python packages to a virtual environment.
To install additional packages only to your virtual environment, enter the following command where yourenvname is the name of your environemnt, and [package] is the name of the package you wish to install. Failure to specify “-n yourenvname” will install the package to the root Python installation.
Deactivate your virtual environment.
Save all your requirements
Add the conda python kernel to jupyter