No module named 'passlib' - windows

How to fix
from passlib.hash import sha256_crypt ImportError: No module named
'passlib'
I have already installed in using pip install passlib
and it says
Requirement already satisfied (use --upgrade to upgrade): passlib in
c:\python34\lib\site-packages Cleaning up...
How do you fix this
thanks

There is an import resolution "issue" with passlib, but I expected that it would not find sha256_crypt instead of not finding passlib.
Firstly, I would ensure that you have the passlib module properly installed on your machine. Secondly, I would try to run the program with the error and see if you can run something like:
sha256_crypt.encrypt("someString")
If that runs, then the only "problem" is that the import resolution is static and it cannot resolve functions which are not defined at run time. This will make sense if you take a look at hash.py from passlib.
# NOTE: could support 'non-lazy' version which just imports
# all schemes known to list_crypt_handlers()
#=============================================================================
# import proxy object and replace this module
#=============================================================================
from passlib.registry import _proxy
import sys
sys.modules[__name__] = _proxy
#=============================================================================
# eoc
#=============================================================================
As you can see, sha256_crypt is not defined here, so the import comes back as being wrong, even though the module will load correctly at run time!
You have two options at this point. If you are using PyDev like I am, you can add an ignore flag next to the import:
from passlib.hash import sha256_crypt ##UnresolvedImport
You can also modify hash.py such that you define a placeholder sha256_crypt function to ensure that the import comes back as valid, but really this is not the best approach, but it does work:
# NOTE: could support 'non-lazy' version which just imports
# all schemes known to list_crypt_handlers()
#=============================================================================
# import proxy object and replace this module
#=============================================================================
def sha256_crypt():
pass
from passlib.registry import _proxy
import sys
sys.modules[__name__] = _proxy
#=============================================================================
# eoc
#=============================================================================
That will ensure that the import resolution process will see that the function exists and it will not complain.

Related

How to Access Settings file from .py file - DRF

This is my project structure. I am trying to access settings.py file from lookups.py
I am importing using following code,
import os
import sys
import django
sys.path.append('/path/to/django/project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manageemployee.settings')
django.setup()
from manageemployee.apps.employee.hrmmongodb import DBMixin
from manageemployee.settings import EVENT_STORE_DICT
But I am getting the following error,
ModuleNotFoundError: No module named 'manageemployee'
It's better to use settings by importing the object django.conf.settings wherever it was needed instead of the settings file itself as you are trying,
Example:
from django.conf import settings
if settings.DEBUG:
# Do Something
So in your use case:
from django.conf import settings
EVENT_STORE_DICT = settings.EVENT_STORE_DICT
# Do Something
Note that django.conf.settings isn’t a module – it’s an object. So
importing individual settings is not possible:
from django.conf.settings import DEBUG # This won't work.
You can also find the detailed information in the Docs

How to import python script using ROS and unittest

I'm facing an issue in a ROS (Python 2.7) project.
Strucure
I'm working on a ROS project. The structure is the following:
probable-adventure
|
|-catkin_ws
|-src
|-ros_rover
|-include
|-launch
|-msg
|-__init__.py
|-Commands.msg
|-Teleoperation.msg
|-scripts
|-__init__.py
|-keyboard.py
|-test
|-__init__.py
|-testing.py
|-__init__.py
|-CMakeLists.txt
|-package.xml
Keyboard node (secondary problem)
keyboard.py starts as follows:
#!/usr/bin/env python
import rospy
from ros_rover.msg import Teleoperation
I describe it as a secondary problem because if I compile the project from catkin_ws directory using catkin_make command, everything works well and I can run the keyboard ROS node perfectly even though I get a pylint error in the from ros_rover.msg ... line.
Unable to import 'ros_rover.msg'
Main Problem
I just wrote the previous as context. The point is that I have to test the code within the scripts directory. I'm using unittest and I create python files in test directory.
I have create the following simple test (testing.py):
#!/usr/bin/env python
import unittest
from scripts.keyboard import GetKey
class TestBareBones(unittest.TestCase):
def test_one_equals_one(self):
self.assertEquals(1, 1, "1!=1")
if __name__ == '__main__':
unittest.main()
I think I have a problem with the imports because if I run the following python commands (from the ros_rover directory):
user#dubuntu:~/probable-adventure/catkin_ws/src/ros_rover$ python testing.py
user#dubuntu:~/probable-adventure/catkin_ws/src/ros_rover$ python -m testing
I get the following error:
File "/home/diego/Documents/probable-adventure/catkin_ws/src/ros_rover/test/prueba.py", line 5, in <module>
from scripts.keyboard import GetKey
File "scripts/keyboard.py", line 14, in <module>
from ros_rover.msg import Teleoperation
ImportError: No module named ros_rover.msg
My thought
I think the problem is related with imports because I have read that python imports depends on the directory from where you run the python command.
I run the test from ros_rover directory because the test's import is scripts.keyboard so it can found it (scripts is in ros_rover). But when the keyboard.py tries to import ros_rover.msg fails because I'm already in ros_rover directory and cannot find ros_rover.
I have read a lot here in StackOverflow and out of it but I didn't find a solution (I have added a empty __init__.py file to ros_rover, scripts and test directories).

how to use pssh python script using Python ProtoBuf library?

I am not able to figure out how Python ProtoBuf library is related to use pssh python script in this documentation https://github.com/google/shaka-packager/tree/master/packager/tools/pssh
How can I build the pssh.py script without the proto file?
You can either use the build-script with the shaka-packager, or you can simply generate the python protobuf files from the widevine header proto file directly.
The Protocol Buffers tutorial for python describes nicely how to use protoc to compile the necessary python code.
If you don't want or need any of the other shaka-packager stuff, but just want to use the pssh.py, then you can just modify this part:
# Append the local protobuf location. Use a path relative to the tools/pssh
# folder where this file should be found. This allows the file to be executed
# from any directory.
_pssh_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(_pssh_dir, '../../third_party/protobuf/python'))
# Import the widevine protobuf. Use either Release or Debug.
_proto_path_format = os.path.join(
_pssh_dir, '../../../out/%s/pyproto/packager/media/base')
if os.path.isdir(_proto_path_format % 'Release'):
sys.path.insert(0, _proto_path_format % 'Release')
else:
sys.path.insert(0, _proto_path_format % 'Debug')
try:
import widevine_pssh_data_pb2 # pylint: disable=g-import-not-at-top
except ImportError:
print >> sys.stderr, 'Cannot find proto file, make sure to build first'
raise
Just keep import widevine_pssh_data_pb2 and make sure that the code you generated with protoc is in the path of the pssh.py file. Then it should work nicely.
What are you planning to use the pssh.py for?

Internal packages in Go

How to import internals packages in Go ?
import (
"runtime/internal/atomic"
"runtime/internal/sys"
)
Like this without get a error:
imports runtime/internal/atomic: use of internal package not allowed
And use internal funcs in a main package ?
Background
Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.
A rule proposed to Go 1.4
An import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.
Short answer
You can't (at least easily) and you shouldn't.
I will show you how I use internal nettest package:
// I copied nettest to vendor with `dep ensure` I think. Then:
mkdir vendor-local
cp -r vendor/golang.org/x/net/internal/nettest ./vendor-local/nettest
vim ./vendor-local/nettest/stack.go and remove import comment // import "foo" [1]
// Use full import in your go file:
import "github.com/foo-user/bar-project/vendor-local/nettest"
[1]: https://github.com/golang/net/blob/a8b9294777976932365dabb6640cf1468d95c70f/internal/nettest/stack.go#L6
Docs about import comments
You may find all import comments in your internal package with grep -r "// import" ./vendor-local/nettest
Why can't I copy nettest to ./vendor and use shorter import
You can, but utils like dep ensure that don't support local packages will purge your copy.

Q: Getting Build Error "Invalid Import Path"

I'm stuck on running the BeeGO app using "bee run" it says
The thing is I've already have setup properly my GOPATH to D:/Web Dev/GO/BeeGO/test-project/
and also routers path does exist and I've tried to manual build the file but it doesn't generate an .exe file.
Anyone knows how to fix this?
I'm using Windows 8.1 Pro (64-bit)
Thanks
GO expects the directory structure under $GOPATH in following ways as described in code organization:
$GOPATH/src <--- where your source code goes
/pkg
/bin
Instead of placing your source files directly under $GOPATH (D:/Web Dev/GO/BeeGO/test-project/ for your case), you want to move your code under $GOPATH/src.
D:/Web Dev/GO/BeeGO/test-project/src/main.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/routers/routers.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/controllers/controllers.go
import path should be always starting from $GOPATH/src. routers.go can be always imported as import "quickstart/routers" and controllers.go can be imported as import "quickstart/controllers".
That's not how you import a package.
The import path is relative to $GOPATH/src. use:
import "quickstart/routers"
Finally fixed the bug from the framework,
What I did:
in main.go import from
"D:/Web Dev/GO/BeeGO/test-project/quickstart/routers"
I changed it to _"../quickstart/routers" make sure to include _ this means to import the library even if it is not used,
Then in the routers/router.go I changed the import path
"D:/Web Dev/GO/BeeGO/test-project/quickstart/controllers" to "../controllers"
It seems BeeGO doesn't generate the template properly and caused the build to fail.
Another possiblity for this error, is when you copy-paste code from the internet,
and
import "quickstart/routers"
became
import "quickstart/routers "
due to bugs in some CMS/Blog systems (notice the space at the end before the closing quote...).

Resources