Install success but has a keyerror when import

I installed the zipline but there is a key error when I import zipline.
I wonder if anyone could help.

KeyError Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 from zipline.api import order, record, symbol

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline_init_.py:29, in
27 from .utils.numpy_utils import numpy_version
28 from .utils.pandas_utils import new_pandas
—> 29 from .utils.run_algo import run_algorithm
31 # These need to happen after the other imports.
32 from .algorithm import TradingAlgorithm

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\utils\run_algo.py:24, in
22 from zipline.finance import metrics
23 from zipline.finance.trading import SimulationParameters
—> 24 from zipline.pipeline.data import USEquityPricing
25 from zipline.pipeline.loaders import USEquityPricingLoader
27 import zipline.utils.paths as pth

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\pipeline_init_.py:1, in
----> 1 from .classifiers import Classifier, CustomClassifier
2 from .domain import Domain
3 from .factors import Factor, CustomFactor

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\pipeline\classifiers_init_.py:1, in
----> 1 from .classifier import (
2 Classifier,
3 CustomClassifier,
4 Quantiles,
5 Everything,
6 Latest,
7 )
9 all = [
10 “Classifier”,
11 “CustomClassifier”,
(…)
14 “Quantiles”,
15 ]

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\pipeline\classifiers\classifier.py:22, in
16 from zipline.pipeline.dtypes import (
17 CLASSIFIER_DTYPES,
18 FACTOR_DTYPES,
19 FILTER_DTYPES,
20 )
21 from zipline.pipeline.sentinels import NotSpecified
—> 22 from zipline.pipeline.term import ComputableTerm
23 from zipline.utils.compat import unicode
24 from zipline.utils.input_validation import expect_types, expect_dtypes

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\pipeline\term.py:45, in
32 from zipline.utils.numpy_utils import (
33 bool_dtype,
34 categorical_dtype,
(…)
37 float64_dtype,
38 )
39 from zipline.utils.sharedoc import (
40 templated_docstring,
41 PIPELINE_ALIAS_NAME_DOC,
42 PIPELINE_DOWNSAMPLING_FREQUENCY_DOC,
43 )
—> 45 from .domain import Domain, GENERIC, infer_domain
46 from .downsample_helpers import expect_downsample_frequency
47 from .sentinels import NotSpecified

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\pipeline\domain.py:27, in
23 import pytz
25 from zipline.utils.calendar_utils import get_calendar
—> 27 from zipline.country import CountryCode
28 from zipline.utils.formatting import bulleted_list
29 from zipline.utils.input_validation import expect_types, optional

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\country.py:10, in
6 def code(name):
7 return countries_by_name[name].alpha2
—> 10 class CountryCode(object):
11 “”“A simple namespace of iso3166 alpha2 country codes.”""
13 ARGENTINA = code(“ARGENTINA”)

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\country.py:55, in CountryCode()
53 TAIWAN = code(“TAIWAN, PROVINCE OF CHINA”)
54 THAILAND = code(“THAILAND”)
—> 55 TURKEY = code(“TURKEY”)
56 UNITED_KINGDOM = code(“UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND”)
57 UNITED_STATES = code(“UNITED STATES OF AMERICA”)

File ~\anaconda3\envs\env_zipline\lib\site-packages\zipline\country.py:7, in code(name)
6 def code(name):
----> 7 return countries_by_name[name].alpha2

KeyError: ‘TURKEY’

1 Like

Good day,

I got the exact same error message yesterday when I was just about to ingest data

Today when trying to import Zipline, I got the error again both on Python v3.7 and v3.8.

I think there is an issue with the package itself that its developers must resolve who are familiar with the modules and code that comprise the library.

It is the first time that I am unable to resolve an error message in my code using Zipline.

Stefan should be able to figure out what the issue is and suggest a solution.

Regards

Thanks for your reply. When I looked at the error message, I saw the country code has an error. Then I could find any related material on how to fix this. Hope someone could figure it out

Here is the solution I found on Github:

ISO3166 has been updated, “Turkey” changed to “Türkiye”. This causes various things to break that relied on “TURKEY”, setting ISO==2.02 works. This is not necessarily a zipline problem, but can cause issues for fresh installs. Posting here in case anyone else has the problem. pip install iso3166==2.02 solves it

2.1.0 - July 11, 2022
-Updated entries
—“Turkey” changed to “Türkiye”

1 Like

Update iso3166 to a newer version after Zipline installation

!pip install iso3166==2.0.2

The import should work:

1 Like