Subproccess cannot execute files with pandas - python

I am working on some projects these days. I have created 3 executable python files in my project called,
crawler.py
process_data.py
process_csv.py
Then I have created run.py to execute the above three, one after one.
Problem -: when I tried to execute the process_csv.py file using run.py, it showed me
Traceback (most recent call last):
File "processors/process_csv.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Interesting point -: But, if I tried to execute process_csv.py separately without using run.py, it was run without any pandas error.
process_csv.py
import pandas as pd
# my code
run.py
import subprocess
subprocess.run(['python', 'processors/process_data.py']) # this line is working fine
subprocess.run(['python ', 'processors/process_csv.py']) # error occur in this line

When I hardly navigate through Stackoverflow, I found a question that has some kind of relation to my question. I realized accepted answer of that question; can be applied to my question also. Then I have applied some changes to my script.
run.py
import subprocess
import sys
subprocess.run(['python', 'processors/process_data.py'])
subprocess.Popen([sys.executable, "processors/process_csv.py"]).communicate()
This solution 100% worked for my script.

Related

ModuleNotFoundError: No module named 'h2oaicore'

I am following the tutorial of driverless: Driverless AI Standalone Python Scoring Pipeline, you can check it in the following link:
http://docs.h2o.ai/driverless-ai/latest-stable/docs/userguide/scoring-standalone-python.html#tar-method-py
I am performing:
Running Python Scoring Process - Recommended
but, when running the last step:
DRIVERLESS_AI_LICENSE_KEY = "pastekey here" SCORING_PIPELINE_INSTALL_DEPENDENCIES = 0 /path/to/your/dai-env.sh ./run_example.sh
the following error happens:
Traceback (most recent call last): File "example.py", line 7, in
from scoring_h2oai_experiment_5fd7ff9c_b11a_11eb_b91f_0242ac110002 import Scorer File
"/usr/local/lib/python3.6/dist-packages/scoring_h2oai_experiment_5fd7ff9c_b11a_11eb_b91f_0242ac110002/init.py",
line 1, in
from .scorer import Scorer File "/usr/local/lib/python3.6/dist-packages/scoring_h2oai_experiment_5fd7ff9c_b11a_11eb_b91f_0242ac110002/scorer.py",
line 7, in
from h2oaicore import application_context
ModuleNotFoundError: No module named 'h2oaicore'
--
Hope you can help me, thanks in advance.
Apparently it doesn't find your h20aicore module. One brute force method to import it could be do the following in your python script:
import sys
import os
sys.path.append(os.path.abspath("path/to/module"))
or alternatively you should add it to your python path.
I agree with #BlackPhoenix's comment. It is looking for h2oaicore module. The DAI Python scoring pipeline comes with a h2oaicore .whl file. Check out the shell script, run_example.sh. It should contain steps about pip installing h2oaicore.

Python ValueError: attempted relative import beyond top-level package

I am trying to make my code Python code run, however, I am running into import errors (shown below). I have looked up some possible solutions and no luck after trying them out.
My directory level is fairly simple.
Python
pkgA
__init__.py
verify.py
console.py
I used to be able to run console.py and was working fine, however, I've been trying to clean up the imports and make the python package installable.
When I run console.py it throws the following error.
Traceback (most recent call last):
File "/Repositories/Python/pkgA/console.py", line 3, in <module>
from ..pkgA import verify
ValueError: attempted relative import beyond top-level package
My console.py has this import from ..supv import verify
I've tried manipulating the paths to try to make it work, but it hasn't been successful.

Error on calling a function from another file on python

I'm getting an error on importing a function from another file in python.
This is my file structure
In checkSpellings.py there's a function defined as spellchecker(tokenized_text) . And I'm trying to use it by importing it on main.py by following code
from checkSpellings import spellChecker
But it gives me an warning (red underline on top of both of checkSpellings(file name) and spellChecker(function name) in import code). This previously happened and I though this is purely an issue with intellisense . Because last time same thing happened (gave me the warning), but the code worked fine.
But now when I run the main.py it gives me an error saying
Traceback (most recent call last):
File "/home/pankaja/Documents/PycharmProjects/BookDigitizer/OCR_Correction/main.py", line 1, in <module>
from checkSpellings import spellChecker
ImportError: cannot import name 'spellChecker
How can I fix this ? What have I done wrong ?
IMPORTANT : Here I'm using python3.6 interpreter on an anaconda virtual environment. Might it be an issue ?
The function is spellchecker but you tried to import spellChecker.

How can I import 2 module in Python?

Guys I'm new in Python and I'm trying to understand modules.I have a folder in desktop and there are 2 module in that folder. One of them is pr.py ,it's takes a array and displays it.And other one is fillArray.py.Now I want to add these 2 friend into interpreter but when I used in interpreter import pr.py or import fillArray it's giving to me this error
>>> import pr
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pr
ImportError: No module named pr
Then if I clicked f5(run) in pr.py module ,write again in interpreter import pr it works.That's ok.But trying to run fillArray.py module same steps, it's restart interpreter and it works butpr.py module is removing.How can I handle this?By the way this can be unnecessary but I'm using python2.7.
Edit:I wrote print.py sorry it should be pr.py
If you have both the modules in the same folder, you can use . which specifies current directory as follows:
from .pr import *
This statement will import all functions of print.py which is in the current directory
If you want specific things to be imported, just specify using their names
from .pr import myfunc1, myfunc2, myclass1, myclass2
If you wish to import the whole module, use:
from . import pr
Hope this helps :)

Import and python source file in folders

I have some troubles understanding how python import works.
I just forked this repository and I am going to play with it but i am having troubles understanding how python import works with file in directories.
The directory structure is the following:
fitbit/
__init__.py
gather_keys_cli.py
api.py
exceptions.py
utils.py
As a preliminary step I need to run gather_keys_cli.py. It depends on api.py, which in turn depends on exceptions.py.
If I try to run it from the python interpreter everything works as expected. If I try to run it from the command line (As suggested in the documentation) I have obtain the following exception.
$./fitbit/gather_keys_cli.py KEY SECRET
Traceback (most recent call last):
File "./fitbit/gather_keys_cli.py", line 34, in <module>
from api import FitbitOauthClient
File "/Users/mariosangiorgio/FitBitHacks/python-fitbit/fitbit/api.py", line 9, in <module>
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
ImportError: No module named fitbit.exceptions
My understanding is that when I invoke the command line the base path is set to the folder containing it. At this point, the scripts correctly imports the oauth client because it uses this instruction from api import FitbitOauthClient, which does not contain any reference to the fitbit directory.
On the opposite, api.py uses this instruction to import exceptions from fitbit.exceptions import (LIST_OF_CLASSES). This instruction contains the reference to the fitbit directory and hence I got the error.
I am not familiar with python so I'd like to understand what is the pythonic way to solve this issue.
I have the feeling that there is something clearly wrong going on and I found a workaround by moving gather_keys_cli.py and changing the api import to import fitbit.api. It works but I am not sure it is the right way to solve the issue I'm experiencing.

Categories