Proper syntax to import module from different directory/folder - python

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

Related

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.

regarding using sys.path.insert and import multiple path information contained in a folder

In demo.py of this project, there is one line of code sys.path.insert(1, 'incl'). After running this line of code, we can see that sys.path has added one more extra item 'incl'. As shown from the github project, we can see there is a subfolder named as 'incl' containing some path information. I can guess that with the help of sys.path.insert, we should be able to include these path information, but running from seg_utils import seg_utils as seg fails by giving the error message of. My estimation is that 'incl' was not correctly imported into sys.path.
Traceback (most recent call last):
File "demo.py", line 52, in <module>
from seg_utils import seg_utils as seg
ImportError: No module named seg_utils

sompy Module Import Error

I can't import the sompy module even though I installed it successfully, and it appears in the modules list of my python environment:
When I try to import the sompy module using the following statement:
import sompy
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sompy'
What's wrong?
Which interpreter/IDE are you using? If using PyCharm, the problem could be that PyCharm hasn't identified a root folder for your .py project.
I would try and recreate the root folder as a subfolder and right click > Make source directory.

Python import module works in prompt, but not in script

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

Python cannot see module

Hello guys i have this directory
C:\Python27
--C:\Python27\pysec-master
----C:\Python27\pysec-master\pysec
__init__.py
example.py
models.py
xbrl.py
xbrl_fundamentals.py
I am trying to run this command
from pysec import xbrl
and i am getting an error:
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
from pysec import xbrl
ImportError: No module named pysec
How is this possible? This code was written by a professional and you can clearly see the file named pysec
The question has been answered by mskimm in the comments sections and runs as:
Is C:\Python27\pysec-master in sys.path ? Nope? Add it to sys.path as sys.path.append(r'C:\Python27\pysec-master')

Categories

Resources