IPython: ImportError: No module named mymodule - python

I am experience this annoying error message, every time after I have updated my module and try to reload it.
I do have a module mymodule in a package mypackage that has a __init___.py file in it.
When I do
from mypackage import mymodule
everything is ok.
After I update the module and reload it with
reload(mymodule)
Error pops up:
In [4]:
...: reload(constants)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-264a569b44f9> in <module>()
1
----> 2 reload(mymodule)
ImportError: No module named mymodule
To resolve this, I have to kill my interpreter and re-import everything when I want to reload one module, which is extremely time-consuming and annoying. How may I fix it?
PS:
I suspect this is something wrong with PYTHONPATH, but since I am using Python tool for Visual Studio, I cannot find the PYTHONPATH option.
Update
As far as I remember, it seems that things start going wrong immediately after I have this
import os
os.chdir(constants.PROJECT_PATH + '//data//')
in one of the modules. Yet does it really matter?
I don't think it matters, as the path in the brackets is exactly my project path.

Try it:
import os, sys
my_lib_path = os.path.abspath('../../../mypackage')
sys.path.append(my_lib_path)
from mypackage import mymodule
or add your package into PYTHONPATH. For unix it:
$ export PYTHONPATH=/absolute/path/to/mypackage

Is your package in the present working directory?
When the interpreter comes across an import libraryname statement, it looks for libraryname in several locations: the present working directory, directories specified by the PYTHONPATH environment variable, and some installation dependent paths.
So as long as your module is in the present working directory, the interpreter is able to find it. However, once the pwd changes, the interpreter isn't able to find the module anymore, and the import fails. You really have two options:
Install your module in a location where Python can find it. Typically, Python packages are located in /usr/lib on Linux systems (not sure about Windows, but you could easily find out). If you put your package there, then the interpreter will pick it up. I wouldn't recommend doing this by hand; write a simple setup.py script to handle the installation for you.
Tell Python where to explicitly look for the package. This is usually done through the PYTHONPATH environment variable. Set that at the command line before invoking the interpreter (or at the system-level, if you must).
If you can't change PYTHONPATH for some reason, then you can modify the path during runtime:
import sys
sys.path.append(your_directory_here)
This is a pretty ugly way to deal with the problem, so should be a last resort.

Related

Problems importing a python module in Maya and it being recognized

Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:
import sys
for pythonPath in sys.path:
print pythonPath
And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py
In Maya's script editor I am typing the following:
import swap
reload(swap)
swap.printSomething()
I get:
Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #
If I take the same code and throw it into a package...
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
And then call this, it works...
import swapPackage.swap as swap
reload(swap)
swap.printSomething()
Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.
Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:
C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py
but I can have one where the package name is different:
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.
import swap
reload(swap)
print swap.__file__

Local vs Global imports python

I am not seeing an answer to this out there, so apologies if this is a duplicate. Basically, I am trying to understand how to force my interpreter (2.7) to import a module from site packages if there is a conflict. For example imagine you are running python from a directory (top_level) that has the following structure:
top_level
----cool_mod
----init.py
----sweet_module.py
but you have already installed sweet module to site packages. When in this directory (but no others) if you run:
from cool_mod.sweet_module import *
you will import from the local module, not the global one. Can I change this somehow?
This situation might arise from the case:
top_level
setup.py
----cool_mod
----init.py
----sweet_module.py
You can run cool_mod.sweet_module before installing if you working directory is top_level. But after installing you can import cool_mod.sweet_module from anywhere. However, if you ever import from this directory, even after installation, you still import the local copy
Inserting the site package directory at the begining of sys.path, and then import.
Or, use imp.load_source to load a module from specified path.

Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.

How to import module from current non-default directory

I'm using Python 2.7. I'm rather new to the python langauge. I have two python modules - "Trailcrest.py" and "Glyph.py", both in the same folder, but not in the Python27 folder.
I need to import "Trailcrest.py" into "Glyph.py", but I am getting the message that "no such module exists".
Additionally, whatever means I use to import the module needs to not be dependent on a solid-state path. This program is cross-platform, and the path can be changed depending on the user's preferences. However, these two modules will always be in the same folder together.
How do I do this?
If you have Trailcrest.py and Glyph.py in the same folder, importing one into the other is as simple as:
import Trailcrest
import Glyph
If this does not work, there seems to be something wrong with your Python setup. You might want to check what's in sys.path.
import sys
print sys.path
To elaborate a bit on Ferdinand Beyer's answer, sys.path is a list of file locations that the default module importer checks. Some, though not all installations of python will add the current directory or the directory of the __main__ module to the path. To make sure that the paths relative to a given module are importable in that module, do something like this:
import os.path, sys
sys.path.append(os.path.dirname(__file__))
But something like that shouldn't ever make it into a "production" product. Instead, use something like distutils to install the module's package into the python site-packages directory.
This can also be achieved using the environment variable PYTHONPATH which also influences Python's search path. This can be done in a shell script so that the Python files do not need to be altered. If you want it to import from the current working directory use the . notation in bash:
export PYTHONPATH=.
python python_prog.py

python ImportError: No module named primes

I'm really new to Python. I'm trying to import a third party module called primes.py. I have placed this module in C:\Python26\Lib (the location where I installed Python). I then have another file which is trying to import this module. The file attempting to import primes is located at C:\Python26.
In my Python file I have the following two lines:
import primes
import sys
When I run this file, I get the following error:
ImportError: No module named primes
Can anyone help me out?
The module needs to be on your PYTHONPATH or in the same directory as the script, app, or module that is trying to import the module.
I'm not a Windows programmer but if you have placed the module in 'C:\Python26\Lib' and your path is set to 'C:\Python26' you need to add '\Python26\Lib' to your PYTHONPATH. I'm not certain on what the syntax would be but it should be something like 'C:\Python26;C:\Python26\Lib'. Assuming everything is the same on Windows, the subdirectories are not searched automatically.
I think a more appropriate place to put the module is to place it in 'site-packages', I don't know how this is accomplished on Windows. On *nix systems there is a script 'setup.py' that comes with the package/module, and uses 'setuptools' to build and install the package/module for you.
you probably should located this under site-packages directory or a private folder instead. Check your sys.path to understand your import paths.
Put primes.py in the lib/site-packages/ directory.
Also: no need to put your own Python files under the installation directory: I'd advise you to put them somewhere else (where it makes sense).

Categories