Transferring Django from windows to ubuntu - python

I have written a project in windows with Django, It works well in windows, I wanted to transfer it to Ubuntu, first I installed the python3 and then made a virtualenv,
activated it and installed django and required packages, I got some errors but the main irritating one is that, I have a INI file, I use the configobj to read it, it works well in windows but in Ubuntu it give 'Key error' here is my code:
from configobj import ConfigObj
ConfigPath = os.path.join(settings.MEDIA_ROOT, 'Vars.ini')
ConfigParser = ConfigObj(ConfigPath)
print(ConfigParser['FileNames']['MotionName'])
and here is the file:
[FileNames]
MotionName = test1
I also tried to use only lowercase words but still no chance.

I can't believe it, my file was Vars.INI, windows would recognize it since it would take INI as extension but Ubuntu was looking for Vars.ini and it couldn't find it so I change the name from Vars.INI to Vars.ini.

Related

How to unrar files

I am trying to unrar files uploaded by users in django web application. I have gone through different approaches but none of them are working for me.
The below methods work fine in my local OS (Ubuntu 18.04), but n't working in the server.
I am using Python 3.6.
import rarfile - here 'rarfile' is imported from python3.6/site-packages/rarfile. It is giving this error -
bsdtar: Error opening archive: Failed to open '--'
Following the steps suggested here, I installed 'unrar'.
from unrar import rarfile - here it's being imported from python3.6/site-packages/unrar/
But this gave the error - LookupError: Couldn't find path to unrar library.
So to solve the issue, I followed the steps mentioned here, and created UnRaR
executable, and also set the path in my environment. This works fine in local, but unable to understand how to do the same in 'production environment'.
import patoolib - importing from patool
I also tried implementing patool, which worked fine in local but not in production and I am getting this error - patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),
This has become a big blocker in my application, can anyone please explain how to solve the problem.
Code snippet for rarfile -
import rarfile
.....
rf = rarfile.RarFile(file_path)
rf.extractall(extract_to_path)
Code snippet for patool -
import patoolib
...
patoolib.extract_archive(file_path, outdir=extract_to_path)
Patool is the good python library it is simple and easy. A simple example of it is here:
pip install patool
Use pip install patool to install the library
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
Now, in your case the problem is probably that you don't have your unraring tool in the path, indicating it can not be called from the command line (which is exactly what patool does). Just put your rar file folder to the path and you're fine or give the absolute path to the rar file.

ZXing in python FileNotFoundError Problem

I have written a simple Python code to detect qrCode. Code:
import zxing
reader = zxing.BarCodeReader()
barcode = reader.decode('../images/QR_CODE-easy.png')
print(barcode)
Now, when I run it, I get the following error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have check this file location is valid by using cv.imread command. Please let me know if someone has a solution to this problem.
You need to install Java Development Kit.
The readme of ZXing Python wrapper says the following:
Dependencies and installation
Use the Python 3 version of pip (usually invoked via pip3) to install: pip3 install zxing
You'll neeed to have a recent java binary somewhere in your path. (Tested with OpenJDK.)
pip will automatically download the relevant JAR files for the Java ZXing libraries (currently v3.4.1)
You appear to be on Windows (as the error code suggests), which uses the backslash for file paths.
It's not good practice as it won't be widely compatible, but if you're in a hurry and know you won't want to use the code on Mac or Linux, you can use double backslashes:
reader.decode('..\\images\\QR_CODE-easy.png')
Otherwise you should use os.path.join or pathlib (assuming your using Python 3)
import os.path
qr_file = os.path.join("..", "images", "QR_CODE-easy.png")
Or
from pathlib import Path
qr_file = Path("../images/QR_CODE-easy.png")
There are further details of a few options here:
https://medium.com/#ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
Edit: it is also worth confirming that your relative path is indeed correct when starting in your current working directory. You can check the current working directory with: cwd = os.getcwd() . You may want to try an absolute path to your file too, just to confirm whether it works with that first.
More details on cwd here: https://stackoverflow.com/a/5137509/142780

Langdetect Issue With pyinstaller

I'm trying to compile my code in oython that uses the library langdetect into a .app with pyinstaller.
as answered in this question : https://github.com/pyinstaller/pyinstaller/issues/2680
I modified the .spec file to include this :
a = Analysis(
# your other stuff here...
datas=[
('langdetect/utils', 'langdetect/utils'), # for messages.properties file
('langdetect/profiles', 'langdetect/profiles'), # don't forget if you load langdetect as a submodule of your app, change the second string to the relative path from your parent module. The first argument is the relative path inside the pyinstaller bundle.
]
# the rest of your analysis spec...
)
But pyinstaller stil does not find utils. I think that I didn't enter the right path in the tuple, but I don't know what I'm supposed to do (the program is supposed to already 'be' in the libraries directory, so shouldn't it work in theory ?)
I used anaconda anaconda to install langdetect. The problem is that I'm not using my laptop to build the .app file, so I would refer if it's a generic path rather than specific.
thanks !
I had the same issue. The issue was with the langdetect library. I used textblob library instead and it worked perfectly.

eclipse pydev - how to install python modules

Just working my way through a (very good) book call Test Driven Development using Python.
This makes use of Python3.4 by the way. By the way, I am running in a Windows 7 OS.
I've got all the stuff working using a simple text editor and running from the command line... in the course of which in particular I used "pip install" to install Django and Selenium, as per book's instructions.
This created folders "selenium" and "django" under ...\Python34\Lib\site-packages\ ... so I added these to the PythonPath for my Eclipse/PyDev project.
With the correct interpreter selected I then tried to run a file which runs fine on the command line: "> python3 functional_tests.py"... but I get
File "D:\apps\Python34\lib\site-packages\django\http\__init__.py", line 1, in <module>
from django.http.cookie import SimpleCookie, parse_cookie
File "D:\apps\Python34\lib\site-packages\django\http\cookie.py", line 5, in <module>
from django.utils.six.moves import http_cookies
ImportError: cannot import name 'http_cookies'
... to me this looks like a dependency thing... as though "pip install" handles dependency matters in a way just including a single folder doesn't.
Question boils down to this: what's the "proper" way to install a python module using PyDev?
several days later
wow... nothing? Nothing! I suppose this must mean that you either have to add dependencies manually or use something like Ant, Maven or Gradle within Eclipse itself. These latter are not my strong areas, even outside an IDE. Would still be nice to have an answer from a PyDev expert!
Well, pip install should work for PyDev (it should automatically recognize the dependency)...
I.e.: in your use case, the only folder that should be in the PYTHONPATH is D:\apps\Python34\lib\site-packages (and pip should install packages to that folder -- make sure you don't add extra folders for "D:\apps\Python34\lib\site-packages\django" nor anything else inside the site-packages to the PYTHONPATH).
If it's still not working, please check if the module django.utils.six.moves.http_cookies is indeed where you expect it to be. Also, you can print the PYTHONPATH being used in runtime with:
import sys
print('\n'.join(sorted(sys.path)))
To check if that's really what you expect.

How to bundle Python dependancies in IronWorker?

I'm writing a simple IronWorker in Python to do some work with the AWS API.
To do so I want to use the boto library which is distributed via PyPi repository. The boto library is not installed by default in the IronWorker runtime environment.
How can I bundle the boto library dependancy with my IronWorker code?
Ideally I'm hoping I can use something like the gem dependancy bundling available for Ruby IronWorkers - i.e in myRuby.worker specify
gemfile '../Gemfile', 'common', 'worker' # merges gems from common and worker groups
In the Python Loggly sample, I see that the hoover library is used:
#here we have to include hoover library with worker.
hoover_dir = os.path.dirname(hoover.__file__)
shutil.copytree(hoover_dir, worker_dir + '/loggly') #copy it to worker directory
However, I can't see where/how you specify which hoover library version you want, or where to download it from.
What is the official/correct way to use 3rd party libraries in Python IronWorkers?
Newer iron_worker version has native support of pip command.
So, you need:
runtime "python"
exec "something.py"
pip "boto"
pip "someotherpip"
full_remote_build true
[edit]We've worked on our toolset a bit since this answer was written and accepted. The answer from my colleague below is the recommended course moving forward.[/edit]
I wrote the Python client library for IronWorker. I'm also employed by Iron.io.
If you're using the Python client library, the easiest (and recommended) way to do this is to just copy over the library's installed folder, and include it when uploading the package. That's what the Python Loggly sample is doing above. As you said, that doesn't specify a version or where to download the library from, because it doesn't care. It just takes the one installed on your system and uses it. Whatever you get when you enter "import boto" on your local machine is what would be uploaded.
The other option is using our CLI to upload your worker, with a .worker file.
To do this, here's what you'd need to do:
Create a botoworker.worker file:
runtime "binary"
build 'pip install --install-option="--prefix=`pwd`/pips" boto'
file 'botoworker.py'
exec "botoworker.sh"
That second line is the pip command that will be run to install the dependency. You can modify it like you would any pip command run from the command line. It's going to execute that command on the worker during the "build" phase, so it's only executed once instead of every time you run a task.
The third line should be changed to the Python file you want to run--it's your Python worker file. Here's the one we used to test this:
import boto
If you save that as botoworker.py, the above should work without any modification. :)
The fourth line is a shell script that's going to actually run your worker. I've included the one we used below. Just save it as botoworker.sh, and you won't have to worry about modifying the .worker file above.
PYTHONPATH="$HOME/pips/lib/python2.7/site-packages:$PYTHONPATH" python botoworker.py "$#"
You'll notice it refers to your Python file--if you don't name your Python file botoworker.py, remember to change it here, too. All this does is set your PYTHONPATH to include the installed library, and then runs your Python file.
To upload this, just make sure you have the CLI installed (gem install iron_worker_ng, making sure your Ruby version is 1.9.3 or higher) and then run "iron_worker upload botoworker" in your shell, from the same directory your botoworker.worker file is in.
Hope this helps!

Categories