Ubuntu 16.04 python can not import my modules - python

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

Related

Problem when importing a package from another directory

I want to import the function write_dynamic_data located in utilities.py into the file trigger_utilities.py which is lower in the hierarchy, but this error is showing up. How can I do? I'm avoiding to use sys.path as I've read it's a bag practise. I'm using Python 3.8.9 and I thinks it isn't necessary anymore).
Project stucture:
utilities.py
[processing]
trigger_utilities.py
This is the console output:
matidellatorre#MacBook-Pro-de-Mati myCode % /usr/bin/python3 /Users/matidellatorre/Desktop/Development/myCode/processing/trigger_utilities.py
Traceback (most recent call last):
File `"/Users/matidellatorre/Desktop/Development/myCode/processing/trigger_utilities.py", line 7, in <module>`
from ..utilities import write_dynamic_data
ImportError: attempted relative import with no known parent package
Thanks in advance!

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.

Import a file within the same package 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.

python shell windows - import error

I'm trying to import module from local path in Python2.7.10 Shell on Windows
I add local path to sys.path by:
import sys
sys.path.append('C:\download')
next I try to import by:
from download.program01 import *
but I've got this error:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
from download.program01 import *
ImportError: No module named download.program01
On Linux this code works fine.
Does someone know what is wrong?
If download is in your pythonpath, then you should import program01 directly.
Also, please don't import *; it makes things very hard to debug. Just do import program01.
put a file __init__.py in your download folder so that python knows it is a module and do sys.path.append('C:') instead.
If you want to keep just using path and not create a module file (the __init___.py) then just keep your code like that but import doing
import program01

Failure to import module in python

I'm trying to use the twisted.mail package in Python:
root#beagleboard:~/twisted# ls /usr/lib/python2.6/site-packages/twisted/mail/
__init__.py imap4.pyo pop3client.py smtp.py
__init__.pyo mail.py pop3client.pyo smtp.pyo
_version.py mail.pyo protocols.py tap.py
_version.pyo maildir.py protocols.pyo tap.pyo
alias.py maildir.pyo relay.py test
alias.pyo pb.py relay.pyo topfiles
bounce.py pb.pyo relaymanager.py
bounce.pyo pop3.py relaymanager.pyo
imap4.py pop3.pyo scripts
I have twisted.mail installed, and within it is a module called imap4. twisted/mail contains the magic init.py file which makes it a python module.
so i should be able to import from it:
root#beagleboard:~/twisted# python
>>> from twisted.mail import imap4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named twisted.mail
As you can see, I'm doing this on a Beagleboard running Angstrom, but that shouldn't matter, should it? However, I can do exactly the same thing on my Ubuntu 11.10 and it imports fine.
I have verified that I do not have a twisted.py module in my current directory.
What silly mistake am I making?
You're in the package that you're attempting to import. Go up one level first.

Categories

Resources