I can't believe after 8 years and 10,000 hours of using Python that I still get stumped by module import errors!!!! My tree is as follows:
pcode
/general
/latin
/latin
v_reading.py
/other
add_path.py
use_pydub.py
In the add_path.py module I add some modules to the sys.path like so:
import sys
vol='/users/me/'
gen_dir = f'{vol}Documents/pcode/'
sys.path.append(gen_dir)
gen_dir1 = f'{vol}Documents/pcode/byu_corpus/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/latin/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/latin/latin/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/general/'
sys.path.append(gen_dir1)
gen_dir1 = f'{vol}Documents/pcode/other/'
sys.path.append(gen_dir1)
so when I run sys.path I get inter alia:
/Users/me/Documents/pcode/other
/users/me/documents/pcode/
/users/me/documents/pcode/latin/ ## I'm not sure if this is necessary but I did it anyway
/users/me/documents/pcode/latin/latin/
/users/me/documents/pcode/general/
/users/me/documents/pcode/other/
The first lines of the use_pydub.py module are as follows:
import sys
import add_path
from general import *
import latin
from latin.latin.v_reading import focus
I do not get an error when I run the module from pycharm but I do get the following error when I run it from command line:
ModuleNotFoundError: No module named 'latin.latin'
I don't understand why I can import the 'latin' folder but not the 'latin.latin' folder. I also tried renaming the second latin folder to latin2 but that did not help. I should also point out that there is no difference between the sys.path generated by pycharm and the sys.path generated by command line.
Solve the problem by correctly using relative imports:
Delete add_path.py, as it will no longer be useful or relevant.
In use_pydub.py, the appropriate relative imports look like:
import sys
from ..general import *
from .. import latin
from ..latin.latin.v_reading import focus
Ensure that pcode is a top-level package when the code runs. If, for example, use_pydub.py is intended to be a top-level script, run it as a module instead, with the -m flag to Python. Alternately, have a top-level driver script that uses the pcode package appropriately.
Ensure that the package is on the module import path. This doesn't require hacking sys.path, in general. It requires starting the program from just outside pcode, in development; or you can simply rely on the fact that installing the package, per the usual process will put it into one of the default paths where Python looks for third-party libraries. (That's the point of all that packaging stuff, using PyPI and pip etc.)
These things are especially important if you want to package the code and upload it to PyPI for others to use. Absolute paths hacked into sys.path are not going to work on someone else's computer - with a different username, the code installed into who knows which Python installation located who knows where on the disk, etc. Hacking in relative paths is just clumsily reinventing the wheel.
Problem solved. By using the advice of tdelaney I ran print(latin.__file__) I discovered that I was not importing the correct package and that I had earlier downloaded my own package from pypi and placed that package in the site_packages and one of those packages was named latin. That packaged was interfering with importing of the desired package. So thanks for the tip tdelaney.
Related
I am having trouble getting an import statement to work. I am attempting to use this package:
https://github.com/mailgun/talon
I am running the following command:
from talon.signature import EXTRACTOR_FILENAME, EXTRACTOR_DATA
I get the following error:
ImportError: cannot import name 'EXTRACTOR_FILENAME' from 'talon.signature' (system path to file)
While troubleshooting I don't see EXTRACTOR_FILENAME or EXTRACTOR_DATA defined anywhere. I did a search in directory for all files. Is there some sort of convention in python where EXTRACTOR_FILENAME maps to a specific class?
UPDATE: Figured it out, just something as simple as manually defining the 2 constants. The docs weren't exactly clear or I missed it.
For your project the import looks like this:
import talon
from talon import quotations
Put those statements on the top of your file, and it should work.
if you don't have the packages on your system type this in your terminal:
pip install talon
The Github repo also explains this
I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
My goal is to create a python script that loops over cells of an excel document. This is my python script called reader.py, and it works just fine.
import xlrd
import os
exceldoc = raw_input("Enter the path to the doc [C:\\folder\\file.xlsx]: ")
wb = xlrd.open_workbook(exceldoc,'rb')
.... some code....
The problem I'm encountering is attempting to use py2exe to create an executable so this script can be used elsewhere.
Here is my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
setup(name='Excel Document Checker',console=['reader.py'])
I run the following command: python setup.py py2exe
It appears to run fine; it creates the dist folder that has my reader.exe file, but near the end of the command I get the following:
The following modules appear to be missing
['cElementTree', 'elementtree.ElementTree']
I did some searching online, and tried the recommendations here Re: Error: Element Tree not found, this changing my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
options={
"py2exe":{"unbuffered": True,"optimize": 2,
'includes':['xml.etree.ElementPath', 'xml.etree.ElementTree', 'xml.etree.cElementTree'],
"packages": ["elementtree", "xml"]}}
setup(name='Excel Document Checker',options = options,console=['reader.py'])
I'm now getting an error:
ImportError: No module named elementtree
I'm sort of at an impasse here. Any help or guidance is greatly appreciate.
Just some information - I'm running Python 2.6 on a 32 bit system.
You explicitly told setup.py to depend on a package named elementtree here:
"packages": ["elementtree", "xml"]}}
There is no such package in the stdlib. There's xml.etree, but obviously that's the same name.
The example you found is apparently designed for someone who has installed the third-party package elementtree, which is necessary if you need features added after Python 2.6's version of xml.etree, or if you need to work with Python 1.5-2.4, but not if you just want to use Python 2.6's version. (And anyway, if you do need the third-party package… then you have to install it or it won't work, obviously.)
So, just don't do that, and that error will go away.
Also, if your code—or the code you import (e.g., xlrd) is using xml.etree.cElementTree, then, as the py2exe FAQ says, you must also import xml.etree.ElementTree before using it to get it working. (And you also may need to specify it manually as a dependency.)
You presumably don't want to change all the third-party modules you're using… but I believe that making sure to import xml.etree.ElementTree before importing any of those third-party modules works fine.
I'm working on a plugin architecture and need to convert a package name like "foo.bar" to the absolute path where the code resides. imp.find_module seems to do what I want, except when the code in question is installed via an egg-link (installed via 'pip install develop').
If there are two modules foo.bar and foo.bar2 which are installed via egg-links (and which live at completely separate file system locations like /home/bob/foo/bar and /home/alice/foo/bar2), find_modules doesn't work because I look up the package "foo" and get the location to foo/bar, but not foo/bar2.
Anyone have suggestions for an alternative function? find_modules doesn't accept hierarchical names, so I can't just pass "foo.bar2" into it.
The easiest way would be to just import the module and inspect its __file__ attribute:
import os
import foo.bar
print(os.path.abspath(foo.bar.__file__))
For dynamic imports:
import os
import sys
module_name = 'foo.bar'
__import__(module_name)
print(os.path.abspath(sys.modules[module_name].__file__))
how can i import other external libraries in web2py? is it possible to
load up libs in the static file?
can somebody give me an example?
thanks
peter
If the library is shipped with python, then you can just use import as you would do in regular python script. You can place your import statements into your models, controllers and views, as well as your own python modules (stored in modules folder). For example, I often use traceback module to log stack traces in my controllers:
import traceback
def myaction():
try:
...
except Exception as exc:
logging.error(traceback.format_exc())
return dict(error=str(exc))
If the library is not shipped with python (for example, pyodbc), then you will have to install that library (using distutils or easy_install or pip) so that python can find it and run web2py from the source code: python web2py.py. Then you will be able to use regular import statement as described above. Before you do this make sure you installed the library properly: run python interpreter and type "import library_name". If you don't get any errors you are good to go.
If you have a third party python module or package, you can place it to modules folder and import it as shown below:
mymodule = local_import('module_name')
You can also force web2py to reload the module every time local_import is executed by setting reload option:
mymodule = local_import('module_name', reload=True)
See http://web2py.com/book/default/section/4/18?search=site-packages for more information.
In web2py you import external library as you normally do in Python
import module_name
or
from module_name import object_name
I am not sure what you mean by "in the static file"