Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run a script myfile.py in command window, but can run within IDLE by importlib or hand-typing #163

Open
AnthonyTheKoala opened this issue Sep 18, 2020 · 1 comment

Comments

@AnthonyTheKoala
Copy link

I wish to report runtime errors when running a script from your github page at https://github.com/hyperopt/hyperopt-sklearn under the subheading "Complete example using the Iris dataset:".

In summary:

  • the program works if I hand-type the script. NO PROBLEMS
  • the program also works if I use importlib. NO PROBLEMS
  • the program throws runtime errors when I run the myfile.py from the command window running python. RUNTIME ERRORS.

The program is from the github page with subheading "Complete example using the Iris dataset":

from hpsklearn import HyperoptEstimator, any_classifier, any_preprocessing
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np
# Download the data and split into training and test sets
iris = load_iris()
X = iris.data
y = iris.target
test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations
estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest,
                          max_evals=100,
                          trial_timeout=120)
# Search the hyperparameter space based on the data
estim.fit(X_train, y_train)
# Show the results
print(estim.score(X_test, y_test))
# 1.0
print( estim.best_model() )

If I handtype/copy-and-paste the above code OR
If I run the file myfile.py by

import importlib
importlib.import_module('myfile.py')

It works as excepted- excerpt from IDLE output.

estim.fit(X_train,y_train)

  0%|          | 0/1 [00:00>> print(estim.score(X_test,y_test))
0.9333333333333333
>>> print(estim.best_model())
{'learner': RandomForestClassifier(bootstrap=False, ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=2,
                       max_features=0.47788085349322584, max_leaf_nodes=None,
                       max_samples=None, min_impurity_decrease=0.0,
                       min_impurity_split=None, min_samples_leaf=10,
                       min_samples_split=2, min_weight_fraction_leaf=0.0,
                       n_estimators=468, n_jobs=1, oob_score=False,
                       random_state=1, verbose=False, warm_start=False), 'preprocs': (StandardScaler(copy=True, with_mean=False, with_std=False),), 'ex_preprocs': ()}

BUT when I run myfile.py in a command window, eg MS Win 7 dos terminal I get the following

c:\Python38>python myfile.py
WARN: OMP_NUM_THREADS=None =>
... If you are using openblas if you are using openblas set OMP_NUM_THREADS=1 or
 risk subprocess calls hanging indefinitely
  0%|                                    | 0/1 [00:00
... If you are using openblas if you are using openblas set OMP_NUM_THREADS=1 or
 risk subprocess calls hanging indefinitely
  0%|                                    | 0/1 [00:00", line 1, in 
  File "c:\python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "c:\python38\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "c:\python38\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "c:\python38\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from
_path
    main_content = runpy.run_path(main_path,
  File "c:\python38\lib\runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\python38\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Python38\myfile.py", line 31, in 
    estim.fit(X_train, y_train)
  File "c:\python38\lib\site-packages\hpsklearn\estimator.py", line 787, in fit
    fit_iter.send(increment)
  File "c:\python38\lib\site-packages\hpsklearn\estimator.py", line 688, in fit_
iter
    hyperopt.fmin(fn_with_timeout,
  File "c:\python38\lib\site-packages\hyperopt\fmin.py", line 469, in fmin
    return trials.fmin(
  File "c:\python38\lib\site-packages\hyperopt\base.py", line 671, in fmin
    return fmin(
  File "c:\python38\lib\site-packages\hyperopt\fmin.py", line 509, in fmin
    rval.exhaust()
  File "c:\python38\lib\site-packages\hyperopt\fmin.py", line 330, in exhaust
    self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
  File "c:\python38\lib\site-packages\hyperopt\fmin.py", line 286, in run
    self.serial_evaluate()
  File "c:\python38\lib\site-packages\hyperopt\fmin.py", line 165, in serial_eva
luate
    result = self.domain.evaluate(spec, ctrl)
  File "c:\python38\lib\site-packages\hyperopt\base.py", line 894, in evaluate
    rval = self.fn(pyll_rval)
  File "c:\python38\lib\site-packages\hpsklearn\estimator.py", line 645, in fn_w
ith_timeout
    th.start()
  File "c:\python38\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "c:\python38\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "c:\python38\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "c:\python38\lib\multiprocessing\popen_spawn_win32.py", line 45, in __ini
t__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "c:\python38\lib\multiprocessing\spawn.py", line 154, in get_preparation_
data
    _check_not_importing_main()
  File "c:\python38\lib\multiprocessing\spawn.py", line 134, in _check_not_impor
ting_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

In sum:

  • Program works as expected when either hand-writing/copy-and-paste code OR using within IDLE importlib.import_module('myfile.py').
  • BUT does there is runtime error when running the script as python myfile.py

Info:
python: 3.8.5
OS: MS Win 7

Thank you,
Anthony of Sydney

@bjkomer
Copy link
Member

bjkomer commented Mar 16, 2021

This looks to be a Windows specific issue with multiprocessing. The other ways of running the script that work probably implicitly handle the requirements Windows is looking for, but when running the python file you likely need to wrap your main script with the code mentioned in the error:
https://stackoverflow.com/questions/24374288/where-to-put-freeze-support-in-a-python-script/24374798#24374798

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants