Import error even though file is clearly in path - python

I am running a script and I am getting the error:
Traceback (most recent call last):
File "common/tensorflow/run_tf_benchmark.py", line 30, in <module>
from common.base_benchmark_util import BaseBenchmarkUtil
ModuleNotFoundError: No module named 'common'
I am running on an ec2 instance with the following AMI: Deep Learning AMI (Ubuntu 16.04) Version 25.0 (ami-025c308193ac1a136) and I am also working in the tensorflow_p36 anaconda environment that comes from the AMI. My Ubuntu directory is structured as so:
home
--ubuntu
--benchmark_models
--benchmark
--common
--__init__.py
--base_benchmark_util.py
--tensorflow
--run_tf_benchmark.py
--__init__.py
Note, there are other files in the directory, but these are the relevant python files.
Currently, the imports at the top of run_tf_benchmark.py are
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from common.base_benchmark_util import BaseBenchmarkUtil
and the printed sys.path is
['/home/ubuntu/benchmark_models/benchmarks/common/tensorflow', '/home/ubuntu/src/cntk/bindings/python',
'/home/ubuntu/benchmark_models/models/image_recognition/tensorflow/mobilenet_v2', '/home/ubuntu/models',
'/home/ubuntu/models/research', '/home/ubuntu/models/research/slim',
'/home/ubuntu/anaconda3/lib/python36.zip', '/home/ubuntu/anaconda3/lib/python3.6',
'/home/ubuntu/anaconda3/lib/python3.6/lib-dynload', '/home/ubuntu/anaconda3/lib/python3.6/site-packages',
'/home/ubuntu/benchmark_models/benchmarks/common']
I can clearly see common in the python path, but the import error still happens. What am I doing wrong?

Just remove 'common' while importing because you are already in 'common' directory
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from base_benchmark_util import BaseBenchmarkUtil

Alright, I solved the issue. The issue was that I added /home/ubuntu/benchmark_models/benchmarks/common, not /home/ubuntu/benchmark_models/benchmarks/. By import benchmarks, it also allowed all of my other code to import common as well.

Related

Python3 - ImportError: No module named

I have 2 folders:
my_python
code.py
MyCode
TestEntry.py
When I run the following commands:
cd /data/my_python
python3 code.py
The above works.
However, if I in my home folder and then run this:
python3 /data/my_python/code.py
I get the following error:
Traceback (most recent call last):
File "/data/my_python/code.py", line 4, in <module>
from TestEntry import TestEntry
ImportError: No module named 'TestEntry'
Here is the code:
import sys
import os
sys.path.append(os.path.abspath('../MyCode'))
from TestEntry import TestEntry
TestEntry().start(507,"My Param1","/param2",'.xyz',509)
Can you help me how to fix this?
You are adding a relative path to sys with your line sys.path.append(os.path.abspath('../MyCode')). Instead, you need to import relative to that file you are calling. Try this:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from TestEntry import TestEntry
TestEntry().start(507, "My Param1", "/param2", '.xyz', 509)
That happens because, as #mkrieger1 mentioned, your sys.path gets messed up. I have a previous answer here which explains how to set it. By sys.path getting messed up, I mean that python will look in the dir that you are running from, not the dir that the script you are running is in. Here is the recommended method:
import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'MyCode')))
... (your code)
or
import sys, os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'MyCode')))
... (your code)
This way python will look in the dir of the file you are running as well.

ValueError: Attempted relative import in non-package [gem5]

I am using the configuration Simulation.py in configs/common. I get the error
ValueError: Attempted relative import in non-package
The full error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "build/X86/python/m5/main.py", line 438, in main
exec(filecode, scope)
File "configs/common/Simulation.py", line 49, in <module>
from . import CpuConfig
ValueError: Attempted relative import in non-package
Simulation.py:
from __future__ import print_function
from __future__ import absolute_import
import sys
from os import getcwd
from os.path import join as joinpath
from . import CpuConfig
from . import BPConfig
from . import MemConfig
import m5
from m5.defines import buildEnv
from m5.objects import *
from m5.util import *
addToPath('../common')
I have tried changing to absolute import paths from relative import paths and I still get error.
This guy had a similar error: https://www.mail-archive.com/gem5-users#gem5.org/msg16430.html but It didn't fix for me.
OS: Ubuntu 18.04.2 LTS
git SHA: d00aa3658498968f7dc2b586347771734af0d24a
After Eleanor's comment, it seems that they were trying to execute configs/common/Simulation.py directly with gem5.opt, which is not supported.
In general, Python scripts under configs/common are not meant to be executed directly, and just factor out other scripts.
The most important in-tree scripts that I am aware of are:
configs/example/fs.py: main cross arch full system script
configs/example/se.py: main cross arch syscall emulation script
configs/example/arm/fs_bigLITTLE.py: ARM bigLITTLE system
configs/example/arm/starter_fs.py: ARM minimal system
gem5/configs/learning_gem5/: tutorial scripts for Jason's Learning Gem5 tutorial
So you see that configs/example/ contains most of the interesting ones. You just have to learn what scripts must contain to be "runnable", the most important component is basically a call to:
Simulation.run
which actually starts the simulation.
The only considerable documentations I know of are:
http://gem5.org/ which is a semi messy wiki, so hit and miss
http://learning.gem5.org/ by Jason which shows how to setup some basic Python configs, and is very valuable
https://github.com/cirosantilli/linux-kernel-module-cheat which contains some setups that just work and can serve as a starting reference

I am getting ModuleNotFoundError: No module named 'pyforms.gui' despite having pyforms-gui installed

I am trying to run a program. It said in it's readme to install requirements first, so I did and it installed pyforms and python_docx
But now when I execute command
python
and i get the error ModuleNotFoundError: No module named 'pyforms.gui'
How can that be fixed?
I tried manually installing pyforms-gui and got message that I already have this module installed.
# -*- coding: utf-8 -*-
import pyforms
from pyforms.controls import ControlButton
from pyforms.gui.controls.ControlEmptyWidget import ControlEmptyWidget #problematic line
from pyforms.gui.controls.ControlProgress import ControlProgress
from generation import Project
from widgets.stage_13 import Stage13Window
from widgets.stage_5 import Stage5Window
from .initial_data_editor import InitialDataEditor
Your import statement is incorrect. The correct imports are given here. Note that pyform.gui is now pyforms_gui.
from pyforms_gui.controls.control_emptywidget import ControlEmptyWidget
from pyforms_gui.controls.control_progress import ControlProgress

Python commpy library error: no module named 'filters'

I have recently installed a library using this code:
pip install scikit-commpy
Moreover, I downloaded the tar.gz file from this site: https://pypi.org/project/scikit-commpy/#files and launch the setup.py file, but when I do this on python to check the installation:
import commpy
It gives me the following error:
File "C:\ProgramData\Anaconda3\lib\site-packages\commpy\__init__.py", line 17, in <module>
from filters import *
ModuleNotFoundError: No module named 'filters'
So it looks like the __init__.py file in that directory has broken imports somehow. I was able to fix it in vim by changing the import to:
init.py
from .filters import *
from .modulation import *
from .impairments import *
from .sequences import *
I'm not sure how that will impact other functionalities in the module, but that does allow me to run
import commpy
with no errors.
NOTE
It appears this behavior is further down in the module as well, so if you were to attempt
from commpy import channelcoding
it will raise similar exceptions. So you will have to do more surgery on the module in ./commpy/channelcoding/__init__.py:
from .convcode import Trellis, conv_encode, viterbi_decode
from .interleavers import *
from .turbo import turbo_encode, map_decode, turbo_decode
from .ldpc import ldpc_decode
from .gfields import *
from .algcode import *
Upon further inspection, the syntax of this library is python2

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

Categories

Resources