How to manually import python modules from site-packages directory? - python-3.x

I have to present my application on another PC, which does not have the modules that I use already installed so I have to locally import everty pythom module that I use. I use a lot of them, like: OpenCV, TesorFlow, Keras, Kivy, sci-kit-image, etc. I discovered that all my python packages are installed in the directory site-packages, which is a part of the tree where is python installed. I want to copy-paste my site-packages directory to the other PC and import all that I need when my app has to run on the other PC. How do I set all modules from site-packages locally to be available for import in my app?
The second solution I could think of would be to download all the modules from the git and official sites, and import them into my app, but it's hard labour and I have to write a .bat file to install them... but I'm pretty sure I don't have the permission to install anything...
Is there a way for my first solution? pls I really need to know
Edit: I use python 3.6

If you copy the whole package directory to your project directory, it should work. It then will be imported as though it is your own module.
Look at this: How does python find a module file if the import statement only contains the filename?

Related

import so file from different folder

I am using ubuntu 20.04 and python3. I want to import so file "ext.so" like this:
impot Ext
from another code. But the so file is in different folder. What is the right way to do it?
What is the right way to do it?
your project should be structured like so:
-head
--sub1
---Ext.so
--sub2
---caller.py
you should have head the folder containing head in your pythonpath somehow (by installing the python module using distutils, or just having head as your working directory or added by modifying PYTHONPATH in .bashrc, or appending it to sys.path in your script), and you should use
from head.sub1 import Ext
granted that your .so file is a python extension and not some sort of dll, anyone installing your project should be able to run your code without any problems.
however, there is definitely nothing stopping you from adding sub1 to your pythonpath and just import Ext.
Edit: i am sorry, if head is in pythonpath, you only need to import from sub1, not head, so you should have the folder containing head in your pythonpath, my bad.

Project directory accidentally in sys.path - how to remove it?

I don't know how it happened, but my sys.path now apparently contains the path to my local Python project directory, let's call that /home/me/my_project. (Ubuntu).
echo $PATH does not contain that path and echo $PYTHONPATH is empty.
I am currently preparing distribution of the package and playing with setup.py, trying to always work in an virtualenv. Perhaps I messed something up while not having a virtualenv active. Though I trying to re-install using python3 setup.py --record (in case I did an accidental install) fails with insufficient privileges - so I probably didn't accidentally install it into the system python.
Does anyone have an idea how to track down how my module path got to the sys.path and how to remove that?
I had the same problem. I don't have the full understanding of my solution, but here it is nonetheless.
My solution
Remove my package from site-packages/easy-install.pth
(An attempt at) explanation
The first hurdle is to understand that PYTHONPATH only gets added to sys.path, but is not necessarily equal to it. We are thus after what adds the package into sys.path.
The variable sys.path is defined by site.py.
One of the things site.py does is automatically add packages from site-packages into sys.path.
In my case, I incorrectly installed my package as a site-package, causing it to get added to easy-install.pth in site-packages and thus its path into sys.path.

Can i use RDkit with pure python3 in Windows OS

I want to use RDkit with python3 in windows OS
But here are something I got confused
Besides the author said the most recommend way is anaconda(I don't want this),the other way is to install RDkit with release binary package. But as I follow the document as BELLOW:
Get the appropriate windows binary build from: https://github.com/rdkit/rdkit/releases
Extract the zip file somewhere without a space in the name, i.e. C:\
The rest of this will assume that the installation is in C:\RDKit_2015_09_2
Set the following environment variables:
RDBASE: C:\RDKit_2015_09_2
PYTHONPATH: %RDBASE% if there is already a PYTHONPATH, put ;%RDBASE% at the end.
PATH: add ;%RDBASE%\lib to the end
I do got the latest releases
I unzip the package
But the directory doesn't have the sub dir name "lib" in last step: PATH: add ;%RDBASE%\lib to the end
What can I do? do I mkdir named lib at there? Or just the binary package is not full?
besides, if i want to use compile source code way,the example enviroment is python2.7,can i compile it with python3?
i have fix this problem
the result i put on my github

Is it possible to have a setup.py instead of a SConstruct?

I am taking a close look at Scons and something smells. SCons uses SConstruct files as base configuration file. This configuration file is a Python file but:
It does not have the .py extension
It does not have any import directives
It is not possible to have auto-completion from IDEs
It it possible to use a variant of the SConstruct file where I could find something like the following?
# build.py
import scons
env = scons.Environment()
env.Program('foo')
It would be not simple (but possible) to do what you're asking. SConscripts are plain python, however the globals available in the context of SConstruct or SConscripts are carefully constructed.
Any user can add methods and also pass python objects into the SConscripts via Export() or exports (in a SConscript call).
That said try:
from SCons.Script import *
That should get you some of what you're looking for.
The fact that Pycharm cannot find the symbols in question doesn't mean the it is not a plain python file.
Additionally I'm not sure how the subject of your question relates to the contents of your question. Typically setup.py is a file to build packages ala setuptools and install via pip (or similar).
Sure you can build whatever you are trying to build with setuptools, it will likely be harder to do, but that said if you get it to work, perhaps easier to upload and distribute via pypi
p.s. It's SCons not Scons.

Packaging Multiple Python Files

I currently am using this guide to package up my project wasp. However currently everything lives inside of the wasp file.
That's not ideal. I would rather have all the classes in separate files so it can be more effectively managed. I have the series of files needed in the debian directory. But I'm not sure how to configure the packaging to package multiple files.
Is there a way to change my packaging to package more than just the one script file?
I'm not a debian package or Python expert, but one way would be to copy the various source files to another location (outside of /usr/bin), and then have /usr/bin/wasp call out to them.
Say you put all of your python code in src/ in the root of your repo. In the debian/install file, you'd have:
wasp usr/bin
src/* usr/lib/wasp/
You'd then just need /usr/bin/wasp to call some entry point in src. For example,
#!/usr/bin/python3
import sys
sys.path.append('/usr/lib/wasp/')
import wasp # or whatever you expose in src
# ...
Again, I don't know the best practices here (either in directory or python usage) but I think this would at least work!

Resources