Import a file within the same package python - python

I'm using Python 3.6
my file structure:
ACS-backend
ACS
-__init__.py
-main.py
-VCDN.py
bin
data
docs
venv
weights
-.gitignore
-requirements.txt
-setup.py
I'm trying to import VCDNN in my main.py with from ACS.VCDNN import VCDNN I have tried with just .VCDNN from VCDNN and just VCDNN from VCDNN the last one used to work before ive added the ACS folder.
To run it from cmd i simply do venv/Scripts/activate.bat to activate my current VENV and then just python main.py and the error I get is:
Traceback (most recent call last):
File "main.py", line 5, in <module>
from ACS.VCDNN import VCDNN
ModuleNotFoundError: No module named 'ACS'
Though when running from PyCharm i see that it executes:
C:\work\COMP1682\ACS-backend\venv\Scripts\python.exe C:/work/COMP1682/ACS-backend/ACS/main.py
which works fine, but when I run the exact same command from my CMD it still gives me the same error.

Try from .VCDN import VCDNN, that would be correct relative import.

Related

In Python how do I correct this Module Not Found Error

I have the following module structure in my Administrator project folder
StockController.py (in the root project folder)
then in a package called Utilities I have the following two modules
ExcelManipulator.py
StockPurchaseInfo.py
Relevant Code from each module as as follows:
StockController.py
from Utilities.ExcelManipulator
import ExcelManipulatorClass
ExcelManipulator.py
from StockPurchaseInfo import StockInfoClass
class ExcelManipulatorClass:
StockPurchaseInfo.py
class StockInfoClass:
When running the StockController module I get the following error
Traceback (most recent call last):
File "d:\Administrator\StockController.py", line 1, in <module>
from Utilities.ExcelManipulator import ExcelManipulatorClass
File "d:\Administrator\Utilities\ExcelManipulator.py", line 2, in <module>
from StockPurchaseInfo import StockInfoClass
ModuleNotFoundError: No module named 'StockPurchaseInfo'
However when I run the ExcelManipulator.py module I dont get the error (it is able to find the StockPurchaseInfo module) why is this the case?
Do you have an __init__.py in your Utilities directory ?
And in your ExcelManipulator, you've to do this :
from Utilities.StockPurchaseInfo import StockInfoClass (or from .StockPurchaseInfo import StockInfoClass to import relatively)
If you run your programm from the root of your project (and you should do so), modules path are defined from it (from the root of the project), so if you're doing from StockPurchaseInfo import StockInfoClass then python is looking for an StockPurchaseInfo.py in you're root project.

"ImportError: No module" after uploading to IBM-Cloud

So I have a path structure that looks like this:
~/Dropbox/Coding/Python/Chessbotslack/scripts/Flask_interface.py
~/Dropbox/Coding/Python/Chessbotslack/database/spreadsheets.py
The first line of the Flask_interface.py is:
from Chessbotslack.database.spreadsheets import add_game
If I run this from my IDE (PyCharm) it runs just fine; but if I run it from my terminal it throws the error:
Traceback (most recent call last):
File "Flask_interface.py", line 1, in <module>
from Chessbotslack.database.spreadsheets import add_game
ModuleNotFoundError: No module named 'Chessbotslack'
To solve this I did two things:
1) Adding __init__.py to the directories
This did seem to accomplish anything
2) Adding the directory to $PYTHONPATH
In terminal I ran
export PYTHONPATH=$PYTHONPATH:~/Dropbox/Coding/Python
This solved the problem for running it from my terminal, but as expected it did not solve it for my IBM-Cloud. Perhaps it has something to do with the requirements.txt file?
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR File "scripts/Flask_interface.py", line 1, in <module>
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR from Chessbotslack.database.spreadsheets import add_game
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR ImportError: No module named Chessbotslack.database.spreadsheets
Changing to a flat folder structure (every file in the Chessbotslack folder) and changing the import command from:
from Chessbotslack.database.spreadsheets import add_game
to:
from spreadsheets import add_game
solved it. The weird thing is that the IDE (PyCharm) does not recognize this as correct; only after (incorrectly) adding the folder name "Chessbotslack." in front of it the IDE helps out.
Any idea what to do about that?

Run module (both as a module and) locally as script with local import

I am pretty new to python, so I might be missing (and probably am) some critical part of the import system, but I can't for the life of me figure it out at this point. I have a directory structure as follows:
/
/always_run.py
/lib/__init__.py
/lib/data.py
/lib/config.py
File Internals (imports):
/always_run.py
from lib import data
/lib/data.py
from lib import config
yields
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from lib import config as configs
ModuleNotFoundError: No module named 'lib'
Note: I've also tried:
from . import config
yields:
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from . import config as configs
ImportError: cannot import name 'config'
Within data.py I also have:
if __name__ == "__main__":
print("loading myself")
Just to test, but it makes no difference, it never gets to that point
Normally I run my program as python always_run.py and the world is my oyster, but if I try to run data.py directly I always get import failures. (after first cding into the lib directory and running python .\data.py
Is what I am trying to do not possible without adding the local directory to the sys.path, like this(test, works):
import sys
local_path = os.path.dirname(os.path.realpath(__file__))
if local_path not in sys.path:
sys.path.append(local_path)
import config
In the process of writing this, I tested one further thing, cding to the parent directory and running python -m lib.data which works flawlessly. Now my question is one of curiosity, because I still can not find the answer otherwise. Is it not possible to run a local file that is part of a module from the local directory? Or must it be done from another directory?

Ubuntu 16.04 python can not import my modules

I've got a folder named aspects where there are 2 files: Aspects.py and Main.py. I'm using the class from Aspects.py in Main.py. There is a line from aspects.Aspects import Aspects there and on Mac OS I've got no problems running my program. But I need to run it on Ubuntu, so I do this:
python3 Main.py
but get the problem:
Traceback (most recent call last):
File "Main.py", line 1, in <module>
from aspects.Aspects import Aspects
ImportError: No module named 'aspects'
Please, help me!
If your aspects folder structure as follows:
- aspects/
- __init__.py
- Aspect.py
- Main.py
To import Aspect module into your main:
#Main.py
from . import Aspect
Aspect.classname

run nupichelloworld in pycharm

I want to run nupichelloworld (https://github.com/lonesword/nupichelloworld) within pycharm but can't figure out how to force it to see nupic libraries. All sources -- nupic.core, nupic.sources, nupichelloworld etc are located on the same level (/home) (I am using the NUPIC-03-11-2014 image which I took from
https://mega.co.nz).
When I try to run it in console, that's all right, but pycharm tells following:
/usr/bin/python2.7 /home/nupic/nupichelloworld/helloworld.py
Traceback (most recent call last):
File "/home/nupic/nupichelloworld/helloworld.py", line 22, in <module>
import nupic
ImportError: No module named nupic
there is the such imports in the file:
from nupic.research.spatial_pooler import SpatialPooler as SP
import numpy as np
How to solve it? Thanx!
You probably need to configure the project's interpreter paths to include your package.

Categories

Resources