Untangle Spaghetti Python Paths

How to manage your Python Environments

Grab the slides:

slides.com/cheukting_ho/pydata-spaghetti-python

How to find where my Python is installed?

In unix (mac os X included) terminal you can do:

$ which python

For Windows Users, chances are one of these (e.g. Python3.6):

  • C:\Python36
  • C:\Users\(Your logged in User)\AppData\Local\Programs\Python\Python36
$ where python

Or in a CMD:

$ cd \
$ ls *ython* -Recurse -Directory
  • <package>.__path__ lets you identify the location(s) of a specific package: (details)
     

     
  • <module>.__file__ lets you identify the location of a specific module: (difference)

     

  • Run pip show <package> to show package information:

     
$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']
$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.py
$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy

But Wait, I use Anaconda

Anaconda is a free and open-source[5] distribution of the Python and R programming languages for scientific computing
- Wikipedia

It also provide a package manager: Conda

Which you can manage your Python enviroment

conda create --name myenv

Create:

conda activate myenv

Activate:

conda deactivate

Deactivate:

Other tools that you can use to managing your Python enviroments:

pyenv-virtualenv, venv, pipenv, poetry

Have you ever have a piece of code that used to work but does not work now?

I copy some code online / my colleague gave me some code and it does not work.

It works on my computer.

Having crazy error saying it needs version x and I have version y which is reauired by my other code.

Let's give it a try:

Create an enviroment:

conda create --name myenv
On macOS and Linux:
python3 -m venv env

On Windows:
py -m venv env

See if you can import Pandas:

Go to Python terimal:

python
>>> import pandas as pd

Activate your enviroment:

conda activate myenv
On macOS and Linux:
source env/bin/activate

On Windows:
.\env\Scripts\activate

Let's give it a try (con't):

Exit Python:

See if you can import Pandas again (repead the steps and exit Python when done)

Install pandas:

pip install pandas
>>> exit()

See if you can import it

install TerminusDB Python Client (I bet you don't have it originally)

pip install terminusdb-client
>>> import terminusdb_client

Let's give it a try (con't):

Deactivate your enviroment:

conda deactivate
deactivate

Try to see if you can import terminusdb_client again

PyData - Untangle Spaghetti Python Paths

By Cheuk Ting Ho

PyData - Untangle Spaghetti Python Paths

  • 256
Loading comments...

More from Cheuk Ting Ho