When I try to open certain python files in neovim, I get an error:
"pool.py" 667L, 25276C
function provider#python#Call[9]..remote#host#Require[10]..provider#pythonx#Require, line 15
Vim(if):ch 1 was closed by the client
Traceback (most recent call last):
File "/home/user/.pyenv/versions/neovim2/lib/python2.7/site.py", line 67, in <module>
import os
File "./os.py", line 44, in <module>
from __future__ import absolute_import
ImportError: No module named __future__
Failed to load python host. You can try to see what happened by starting nvim with $NVIM_PYTHON_LOG_FILE set and opening the generated log file. Also
, the host stderr is available in messages.
Press ENTER or type command to continue
This happens any time I open a python file in a directory that contains an os.py or os.pyc file. It looks like neovim is trying to import the local os.py file instead of the one in the virtualenv.
What can I do about this?
EDIT: turns out it's not when I open a file in the same directory as an os.py file, it's when I open a file anywhere while the current working directory has an os.py file. Basically, it looks like python is checking the local directory for imports before checking the python libs.
I figured it out. The problem was with my $PYTHONPATH. I had in my .bashrc file this:
export PYTHONPATH="$PYTHONPATH:~/.local/lib/python"
The problem was that, when that line is executed, $PYTHONPATH is empty, leading to the string starting with a :. I'm not 100% sure why, but that resulted in python checking the local directory for a module BEFORE checking the python libraries.
I changed it to
if [ -z "$PYTHONPATH" ]; then
export PYTHONPATH="~/.local/lib/python"
else
export PYTHONPATH="$PYTHONPATH:~/.local/lib/python"
fi
And now it works.
Related
I have a following question:
I have a test function written in Pytest for Django project:
#pytest.mark.django_db
def test_class():
path = Path(r'ascertain\tests\csv')
handler = DatabaseCSVUpload(path, delimiter=',')
handler()
ascertain\tests\test_upload_csv.py:19 (TestUploadCSVtoDatabaseNegative.test_class)
Traceback (most recent call last):
File "C:\Users\hardcase1\PycharmProjects\telephone_numbers\ascertain\tests\test_upload_csv.py", line 27, in test_class
handler()
File "C:\Users\hardcase1\PycharmProjects\telephone_numbers\ascertain\handle_csv.py", line 129, in __call__
for file in self.get_csv_files():
File "C:\Users\hardcase1\PycharmProjects\telephone_numbers\ascertain\handle_csv.py", line 48, in get_csv_files
raise EmptyFolder()
telephone_numbers.custom_exceptions.EmptyFolder:
It basically expects *.csv files be inside directory ‘path’ and handle them to upload to DB.
Problem is that Pytest can’t see any files in this directory as well as in other directories. I have tried multiple times with different folders.
In fact file is there:
list(Path(r'ascertain\tests\csv').glob('*.csv'))
[WindowsPath('ascertain/tests/csv/test.csv')]
Same test function written in unitests works correctly:
from rest_framework.test import APITestCase
class TestCase(APITestCase):
def test_open(self):
path = Path(r'ascertain\tests\csv')
handler = DatabaseCSVUpload(path, delimiter=',')
handler()
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...
Process finished with exit code 0
Question is -what I need to do to make Pytest see this file?
Thank you!
I'm not sure this problem relates to Pytest vs Unittest, rather I think it has to do with where you executing these files from. I think you can resolve this by using an absolute path instead of the relative path you included in the example.
An absolute path will start at your systems root directory instead of starting at the directory you are executing the file from. I'm not a Windows user, but from what I can see online the path to this data should look something like this C:/users/path/to/data.
I have a virtual environment of Python 2.7 with ipython installed (Ubuntu 16.04.2 (Xenial) LTS.)
When I’m working in the virtual environment (after running source venv/bin/activate in bash shell while being in the parent directory of the virtual environment) I have no problem executing conversion of my jupiter’s notebook from bash shell like so:
ipython nbconvert --to html --execute my_notes.ipynb --stdout > /tmp/report.html
But when I’m trying to call that command from fabric’s task using subprocess:
command = ['ipython', 'nbconvert', '--to', 'html', '--execute', notebook_path, '--stdout']
output = subprocess.check_output(command,
cwd=os.environ['PYTHONPATH'],
env=os.environ.copy())
It always fails with this exception I cannot find a reason for it:
Traceback (most recent call last):
File "/opt/backend/venv/bin/ipython", line 7, in <module>
from IPython import start_ipython
File "/opt/backend/venv/local/lib/python2.7/site-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/opt/backend/venv/local/lib/python2.7/site-packages/IPython/core/application.py", line 25, in <module>
from IPython.core import release, crashhandler
File "/opt/backend/venv/local/lib/python2.7/site-packages/IPython/core/crashhandler.py", line 28, in <module>
from IPython.core import ultratb
File "/opt/backend/venv/local/lib/python2.7/site-packages/IPython/core/ultratb.py", line 119, in <module>
from IPython.core import debugger
File "/opt/backend/venv/local/lib/python2.7/site-packages/IPython/core/debugger.py", line 46, in <module>
from pdb import Pdb as OldPdb
File "/usr/lib/python2.7/pdb.py", line 59, in <module>
class Pdb(bdb.Bdb, cmd.Cmd):
AttributeError: 'module' object has no attribute 'Cmd'
More info to save your time.
I’ve tried:
Using same paths for PYTHONPATH as I got from PyCharm run/debug configuration.
Using nbconvert as python library from this documentation.
Tried os.system("ipython nbconvert…").
Wrapped working command (ipython nbconvert…) with a shell script and used it in subprocess.check_output and os.system.
Get drunk and bang my head on a brick wall.
And always end-up with that cursed exception.
Reposting as an answer for completeness:
There was a file called cmd.py somewhere where Python was finding it as an importable module. This was shadowing the cmd module in the standard library, which is used by pdb, which IPython imports. When pdb tried to subclass a class from cmd, that class wasn't there. Moving cmd.py out of the way lets it find the cmd module it needs.
This is an unfortunate annoyance with Python - lots of short words are already used as module names, and using them yourself produces crashes, with a wide range of different errors.
I have first tried to create my original game sample using Pygame.
I have traced the instruction written in the Web site below:
https://irwinkwan.com/2013/04/29/python-executables-pyinstaller-and-a-48-hour-game-design-compo/
Specifically,
I created "myFile" class something like following and read every file (.txt, .png, .mp3, etc...) using this class
myFile Class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
class myFile():
def resource_path(self, relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative)
### code where I read something ###
myfile = myFIle()
filename = myfile.resource_path(os.path.join("some dir", "somefile"
+ "extension")
I typed command below to create .spec file (myRPG.py contains main)
pyinstaller --onefile myRPG.py
I modified .spec file so exe object include Trees (I store data files in separate
directories such as data, image, and so on)
myRPG.spec file
# -*- mode: python -*-
block_cipher = None
a = Analysis(['myRPG.py'],
pathex=['C:\\mygame\\mygame'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
Tree('bgm', prefix='bgm'),
Tree('charachip', prefix='charachip'),
Tree('data', prefix='data'),
Tree('image', prefix='image'),
Tree('mapchip', prefix='mapchip'),
Tree('se', prefix='se'),
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='myRPG',
debug=False,
strip=False,
upx=True,
console=True )
I did rebuild my package using modified .spec file
pyinstaller myRPG.spec
When I execute myRPG.exe file, I got the error below
C:\mygame\mygame\dist>myRPG.exe
[Errno 2] No such file or directory:
'C:\\Users\\bggfr\\AppData\\Local\\Temp\\_MEI64~1\\item.data' IO Errorが発生しました。
Traceback (most recent call last):
File "myRPG.py", line 606, in <module>
File "myRPG.py", line 37, in __init__
File "myItemList.py", line 11, in __init__
File "myItemList.py", line 33, in load
UnboundLocalError: local variable 'fp' referenced before assignment
Failed to execute script myRPG
I believe that I properly specify the directories where data is expanded since I use the function that checks _MEIPASS but it does not work.
I have also tried to use "added_files" instead of Tree did not help for me at all. ”a.datas" may work for small number of files but I do not want to specify all the files I'm going to use, because it will be over hundreds of thousands of files.
This is a very helpful page that I used when converting my pygame program into an executable.
In short:
Here is the place where you can download pyInstaller. Click on the download button under "Installation." Wait for it to download, then run it.
Type this into the popup where it says command: venv -c -i pyi-env-name. The reason for this is stated in the page mentioned at the top.
If you have pyInstaller installed already, type pyinstaller --version to make sure. If you don't, type pip install PyInstaller. If this doesn't work, look at step 7 in the guide up top.
Make sure that you put a copy of your program in the folder that it says on the command line.
If you want a zip with the exe and all necessary files in it type pyinstaller and then the name of your program, complete with the .py extension. However, if you want an exe that doesn't need all of the extra files in the zip that you can just run by itself, type pyinstaller --onefile --windowed and then the name of your program, complete with the extension of .py.
Once you have done this, the exe will appear in a dist folder inside of the one where you placed your program in step 3.
This is how it worked for me, and I apologize if I have misinformed you. Be sure to upvote/mark as answered if this works.
Try to parse a .shp in django shell:
from django.contrib.gis.gdal import DataSource
ds = DataSource('/Users/.../Downloads/Iceland.shp')
get:
GDAL_ERROR 4: Unable to open /Users/.../Downloads/Iceland.shx or /Users/.../Downloads/Iceland.SHX.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/.../lib/python2.7/site-packages/django/contrib/gis/gdal/datasource.py", line 78, in __init__
raise GDALException('Could not open the datasource at "%s"' % ds_input)
GDALException: Could not open the datasource at "/Users/.../Downloads/Iceland.shp"
File exists, chmod is 755, .shx file is correct (tested in online services).
Then I try to test .kml file and it works
OS: Mac OS X 10.10.5
You are missing "Iceland.shx" file. It should be in the same archive as Iceland.shp. Just put it in the same directory. I've tried this files: http://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/iceland-shapefile and catch same error.
If it will not help, there some other options how to debug:
Check the mandatory .shx, .shp and .dbf files are in the same directory.
Check no other programs have the shapefile open (and locked).
Open the shapefile in QGIS/ArcGIS and validate geometry.
Try another shapefile
I need to add that I have the same error when I tried to run the command:
python manage.py ogrinspect /map/data/points.shp BuildingFootprints --srid=4326 --mapping --multi
And the error was generated because of a backslash before the map, so it passed when I run:
python manage.py ogrinspect map/data/points.shp BuildingFootprints --srid=4326 --mapping --multi
When I was looking for my solution I found this question, which is answered. Anyway, I wanted to post this answer for those people who came here like I do a few min ago. Maybe someone help.
python manage.py ogrinspect map/data/points.shp BuildingFootprints --srid=4326 --mapping --multi
Ensure your data is located in the folder... map/data/points.shp BuildingFootprints
I'm deploying my django application onto a CentOS 5.5 server, with django-1.1.4 over python-2.6.5.
I have multiple settings files inside the myapp/settings/ folder.
I would like to run the syncdb; here's what I do (with myapp inside myproject folder):
$> cd /var/www/apps/myproject
$> export PYTHONPATH=/var/www/apps/myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.my_serverconfig
$> python26 myapp/manage.py syncdb
Django then issues an error like this :
Error: Can't find the file 'settings.py' in the directory containing 'myapp/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
Traceback (most recent call last):
File "emon/manage.py", line 17, in <module>
execute_manager(settings)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 360, in execute_manager
setup_environ(settings_mod)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 343, in setup_environ
project_module = import_module(project_name)
File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named my_serverconfig
In the myapp.wsgi file, os.path is appended with myproject path, and the os.environ['DJANGO_SETTINGS_MODULE'] is set also. Apache (through mod_wsgi) can start the app with no such error.
Finally, this works under Windows, where I run python-2.6.6 with django-1.1.1.
$> d:
$> cd d:\Code\myproject
$> export PYTHONPATH=d:\Code\myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.dev_settings
$> python.exe myapp/manage.py syncdb
I know the versions are not the same, but I'm not sure that the minor differences may cause all my woe. Moreover I don't seem to find the exact same python version for Windows.
Any thoughts? Thanks a lot for reading.
O.
EDIT: added the manage.py content
#!/usr/bin/env python
from django.core.management import execute_manager
import os
if __name__ == "__main__":
settings = None
try:
if os.environ.has_key('LOCAL_SERVER_SETTINGS'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings.%s' % os.environ['LOCAL_SERVER_SETTINGS']
if os.environ.has_key('DJANGO_SETTINGS_MODULE'):
settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])
if settings is None:
import settings
execute_manager(settings)
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
import traceback
traceback.print_exc()
sys.exit(1)
EDIT : more on what happens in the myapppackage
I patch some django functions/classes from within the myapp.__init__ module. I was thinking the import django part in this module was causing a circular reference. The code is executed when I load myapp.settings.[any_config] and could have caused the crash. But then, how come the correct settings module is loaded with no error by WSGI, and that it works fine also on Windows? More : after commenting out the said code parts, the ImportError is still there.
If you move your settings file, you need to modify manage.py to tell it where to find it. The default is for it to be in the same directory as manage.py, but if you move into another python module(folder with __init__.py) then you need to update the reference in manage.py.
Look in manage.py where it imports your settings module, and import settings.dev_settings in your case instead. Look for 2 important lines
imp.find_module('settings') # Assumed to be in the same directory.
...
import settings
Change these to reference where you moved the settings file to(assuming you moved to settings.dev_settings):
imp.find_module('settings.dev_settings') # Assumed to be in the same directory.
...
from settings import dev_settings as settings
You can also use the command line option --settings to specify which settings file to use.