Python import module works in prompt, but not in script - python

import weka.core.jvm as jvm
jvm.start()
data_dir = "C:/Data/Python/Weka/Data/"
from weka.core.converters import Loader
loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(data_dir + "logistic.arff")
data.class_is_last()
print(data)
I'm executing the above example code of weka python wrapper from their doc. So I'm sure that there's no problem in the code. All modules are installed. But the code is not working when it is run as script (by pressing F5 in IDLE). It is throwing the following error:
Traceback (most recent call last):
File "C:\Data\Python\Weka\weka.py", line 1, in <module>
import weka.core.jvm as jvm
File "C:\Data\Python\Weka\weka.py", line 1, in <module>
import weka.core.jvm as jvm
ImportError: No module named core.jvm
But the code works when I copy and paste it line by line to the IDLE command prompt. No idea why. Where did I go wrong?

Try changing the name of your file to something that's not the name of any of the module you're importing. For example change weka.py to myscript.py

Related

Proper syntax to import module from different directory/folder

How does one correctly import a module from a folder that your .py isn't in?
I'm using Python 3.8 and
I'm trying to load the module csvread.py in testing.py.
The path to this module is Modules/csv/csvread.py
The command doesn't appear to be: from botmanager.Modules.csv import csvread
The stack trace I get is:
Traceback (most recent call last):
File "C:/Users/QT/PycharmProjects/botmanager/testing.py", line 1, in <module>
from botmanager.Modules.csv import csvread
ModuleNotFoundError: No module named 'botmanager'
Project tree: https://i.imgur.com/sRO0qLM.png
To navigate to scripts in a subfolder, do the following:
import Modules.csv.csvread as csvread

gsutil not working if calling from pycharm with python and subprocess, "cannot import name _common"

I have written a python script to automatize some gsutil operations.
gsutil works fine if I run it by command line. But if I translate the same command with subprocess in python I get an error:
BUCKET_NAME = 'datastore-backup'
FOLDER_NAME = 'my_folder'
gcs_path = os.path.join('gs://', BUCKET_NAME, FOLDER_NAME)
files = subprocess.check_output(['gsutil', 'ls', gcs_path], stderr=sys.stdout)
print(files)
I get this error
Traceback (most recent call last):
File "/Users/dario/Downloads/google-cloud-sdk/bin/bootstrapping/gsutil.py", line 13, in <module>
import bootstrapping
File "/Users/dario/Downloads/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 32, in <module>
import setup # pylint:disable=g-import-not-at-top
File "/Users/dario/Downloads/google-cloud-sdk/bin/bootstrapping/setup.py", line 55, in <module>
from googlecloudsdk.core import properties
File "/Users/dario/Downloads/google-cloud-sdk/lib/googlecloudsdk/core/properties.py", line 34, in <module>
from googlecloudsdk.core.util import times
File "/Users/dario/Downloads/google-cloud-sdk/lib/googlecloudsdk/core/util/times.py", line 55, in <module>
from dateutil.tz import _common as tz_common
ImportError: cannot import name _common
any help
I have solved this problem for good. The source of the issue is when you call either subprocess.check or os.system(, the gsutil cli would use the wrong python.
In my case gsutil uses a system python binary that is 2.7. I uncovered this issue by inserting a print(tz) statement in /Users/dario/Downloads/google-cloud-sdk/lib/googlecloudsdk/core/util/times.py. And it turns out that it is loading a Python2.7/site-packages version of dateutil.
So to fix this, you can specify the cloudsdk python binary via the following
export CLOUDSDK_PYTHON=<what-ever-python-distro-you-want>
pyCharm is a wonderful tool second to non. Hope this makes it easy for some to use the built-in runner/debugger of this powerful IDE.

Import fails in terminal but works in PyCharm

I'm using PyCharm for a project with the following file hierarchy:
And I'm running main.py in PyCharm with the following configurations:
Working Directory: /Users/me/longpath/project/amlproject/pca_mixtures.
When I try to run in terminal, it fails:
~/longpath/project/amlproject/pca_mixtures$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from pca_mixtures.funcs import PCAMixture
ModuleNotFoundError: No module named 'pca_mixtures'
and nothing changes if I jump up to the parent folder:
~/longpath/project/amlproject$ python pca_mixtures/main.py
Traceback (most recent call last):
File "pca_mixtures/main.py", line 2, in <module>
from pca_mixtures.funcs import PCAMixture
ModuleNotFoundError: No module named 'pca_mixtures'
The reason for using from pca_mixtures.funcs import PCAMixture instead of just from funcs import PCAMixture was so that PyCharm would recognize the import and not red-underline it, as I've described here. Now, it seems that this has lead to me not being able to run the project in the terminal.
How would you handle this? I want to be able to run it in the terminal because the PyCharm output is not fully sequential (error messages output before program output) which is annoying when debugging.

Python custom module not found but already exists

I have created a separate file to hold some custom issue classes for my Python file and upon execution of the following command: coverage run test_syntax.py it prints out the following error as seen in this Travis CI build:
Traceback (most recent call last):
File "test_syntax.py", line 9, in
from ros import main as s
File "/home/travis/build/Richienb/ROS-Code/src/ros/main.py", line 57, in
from errors import ConversionError, WrongInput, UnexpectedError
ModuleNotFoundError: No module named 'errors'
You can find all of the code here
Also, I have already cd into the src directory.
You need a relative import like this (second one was needed on my machine to even get to the error you have posted):
# in main.py
from .errors import ...
# ros.py
from . import errors

Old error message keep popping up on IDLE

For some reason, anytime I try to import a python library, a particular old error message keeps popping up.
However, everything works fine when working with shell.
I know this is not a real programming question, but I'm stuck and would appreciate any help.
Any ideas on how to fix this?
Error message
>>> import nltk
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import nltk
File "C:\Python27\lib\site-packages\nltk\__init__.py", line 91, in <module>
from internals import config_java
File "C:\Python27\lib\site-packages\nltk\internals.py", line 22, in <module>
except ImportError: from xml.etree import ElementTree
ImportError: No module named etree
Does "C:\Python27\lib\xml\etree\ElementTree.py" exist in your computer?
Since IDLE and shell behaves differently, you may try the two lines in IDLE and Shell:
import sys
sys.path
Then check the difference of env path

Categories

Resources