Import error when importing files from folder - python

I am trying to running the Sahana Eden software from terminal, but I keep getting an import error.
Traceback (most recent call last):
File "web2py.py", line 18, in (module)
import gluon.weidget
File "C:\Eden\web2py\gluon\__init__.py", line 15, in (module)
ImportError: No module named 'globals'
The globals module is right in the file where it is supposed to be. Below init
So I went into init and I removed the import to see what would happend.
#from globals import current
from html import *
from validators import *
The next local import, html, works fine, but then the next local import, "validators"(which is also right where it should be) gives me an import error as well.

Running python -V should tell you which version of Python you're running. --version is also an option.

Related

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

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?

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

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