python shell windows - import error - python

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

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.

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.

Import error when importing files from folder

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.

import error in python on ubuntu

I have created my first python module on ubuntu. When I'm trying to import the module in python using :
import brian
it is giving error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named brian
I have brian in /home/noamaan and python is in /usr/bin.
If you launch python from the directory that contains brian module, everything will work as it is now.
To import custom module from anywhere you want you should read attentively something on the import mechanism in python to learn where the imported modules are searched for, etc.
But to make your code work right now, I can recommend you the following:
Either extend your PYTHONPATH variable before running python, to include the directory of your module
Or append it right in the code by using sys module in this way.
import sys
sys.path.append("path/to/module/dir")
import brian
Also, see info on site module
by default Python import modules from Python path var.
You can view these paths so:
import sys
print sys.path

Categories

Resources