Embeddable Python - ModuleNotFoundError for project folders - python

I'm trying to create an embedded python for my project.
I downloaded the portable python 3.7.6
Copied the folders from my main python installation (Lib, Scripts, include, DLLs etc..)
Added them to the PTH file like so :
python37.zip
.
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
#import site
and when I tested it, I tried to run a project with the following structure :
By running the command (using the embedded python):
G:\GitHub\VRoscopy\VRoscopy\Conversion\python\python.exe main.py
I get the following error :
> Traceback (most recent call last):
> File "mymain.py", line 1, in <module>
> import folder.module as m ModuleNotFoundError: No module named 'folder'
However, when I try running the system python everything works normally.
Hope anyone can help me, thanks

The problem was solved by adding the project files to the PTH file, and uncommenting the "import site" like follows :
python37.zip . ../invesalius
../invesalius/* # <----- added this line
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
import site

Related

Pyinstaller to package app hidden-import warning/exceptions

So, I've had some trouble setting up my application through Inno setup Compiler, at first I assumed it was a problem within Inno itself but on further inspection I think it is my actual exe. I am able to create a working exe file that runs my program properly but only on my own pc. I am able to create the setup file that also works through Inno setup but it only works on my own pc. I have sent both the actual exe file and the Inno setup file to another computer and downloaded it there and ran it, both meet the same "Fatal Error: failed to run script tk_app.py". Therefore, the problem must be that I have not been able to pavkage the app properly with pyinstaller.
My prgoram has 5 files in total (all in the same folder): main.py, file1.py, file2.py file3.py, tk_app.py
All of them importing each other and using python libraries. I know that pyinstaller supports librarires such as pandas, tkinter and many more without needing the --hidden-impoort= command and that it will pick up all files within the program if there are files that are importing each other.
So I use the file tk_app.py (which contains my tkinter UI and imports main.py which then goes onto import file1.py which import another file so on)
The pyinstaller command line I use to make the exe is as follows:
PS C:\Users\ripta\Desktop\CODING\CSV_Upload> pyinstaller -w --hidden-import=bs4 --hidden-import=fake_useragent --hidden-import=urllib.prase --hidden-import=urllib.request --hidden-import=os --hidden-import=pandas.io.parsers --icon=trademark_icon.ico --onefile tk_app.py
My question is, will pyinstaller tell me when it needs a given --hidden-import='name' when running becuase it doesn not throw up any errors and does produce a spec file, a build folder and a dist folder containing the exe file.
I have noticed that it throws up WARNINGs or Exceptions (also not sure why it mentions django as I do not import or use it at all inthe application) :
59182 INFO: Loading module hook 'hook-django.db.backends.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... 61711 WARNING: Hidden import "django.db.backends.__pycache__.base" not found! 61712 INFO: Loading module hook 'hook-django.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "<string>", line 21, in walk_packages File "c:\users\ripta\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
Or show Hidden-import not found (Of imports I have no idea about):
149329 WARNING: Hidden import "pkg_resources.py2_warn" not found! 149330 WARNING: Hidden import "pkg_resources.markers" not found!
The fact that the script fails to run on any other computer besided my own leads me to think that it must be lacking a dependency that is only found on my computer, therefore I am not using pyinstaller correctly but am not too sure where exactly I am making the mistake.
First I've uninstalled Setuptools and reinstalled it with specific version. Then, I've imported pkg_resources.py2_warn as hidden import :
pip uninstalled setuptools
pip install --upgrade 'setuptools<45.0.0'
pyinstaller --hidden-import='pkg_resources.py2_warn' tk_app.py
It worked for me.

Import from my package recognized by PyCharm but not by command line

I've created the following package tree:
/main_package
/child_package
version.py
where version.py contains a single string variable (VERSION)
Inside my script in child package I'm importing version.py by the following line:
from main_package.version import VERSION
While I'm running the code from PyCharm everything works great, however when I'm running the code via the command line I'm getting the following error message:
C:\Users\usr\PycharmProjects\project\main_package\child_package>python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
from main_package.version import VERSION
ModuleNotFoundError: No module named 'main_package'
I've found in the internet that I might need to add my package to the python path, however it doesn't seems to work for me
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
The pythonic way is to have a setup.py file to install your project in the system (check python Minimal Structure):
from setuptools import setup
setup(name='main_package',
version='0.1',
description='main package',
license='MIT',
packages=['main_package'],
zip_safe=False)
Then you install it as follow:
python setup.py install for global installation
OR
python setup.py develop for local installation in editable mode

Sikulix install - jython

I need to test unity 3d app on mobile device (or emulator). I do not want to press on buttons using coordinates only, so I want to use Sikuli. I will describe steps that I have already done:
Downloaded and installed Sikuli v1.1.1
Installed java 32bit
Installed jython 2.7.0
jython added as interpreter in my project in pyCharm
sikulixapi is run
Copied Sikuli lib from appData to jython folder
Created Python file and added imports
And when I try to run project I receive this error:
D:\jython2.7.0\bin\jython.exe -Dpython.path=D:\python_projects\sikuli_test D:/python_projects/sikuli_test/test_sikuli.py
Tra**ceback (most recent call last):
File "D:/python_projects/sikuli_test/test_sikuli.py", line 1, in <module>
from sikuli import Region
File "D:\jython2.7.0\Lib\sikuli\__init__.py", line 5, in <module>
from org.sikuli.basics import Debug
ImportError: No module named sikuli
Process finished with exit code -1**
What am I doing wrong?
The following works for me:
import org.sikuli.script.SikulixForJython
before
from sikuli import *
In your jython project interpreter, set the environment variables path to where your sikuli jar is placed. That should fix it.
Also make sure to use this in your code
import org.sikuli.script.SikulixForJython
from sikuli import *

how to provide 'make directory as source root' from pyCharm to terminal

I have some problems with migrating to production:
cabox#box-codeanywhere:~/workspace/PEP$ python ./dev_scrapers/jordan.py
Traceback (most recent call last):
File "./dev_scrapers/jordan.py", line 3, in <module>
from utils import create_entity, create_id, custom_opener
ImportError: No module named utils
i have used pyCharm with button 'make directory as source root'
how to execute such command in terminal?
You should add your source root directory to PYTHONPATH:
export PYTHONPATH="${PYTHONPATH}:/your/source/root"
You can set the environment variable PYTHONPATH within the terminal as suggested by the accepted answer. This has to be done every time you start a terminal. In the case you use a virtual environment, you can place the variable assignment, like export PYTHONPATH="/your/source/root", in the file venv/bin/activate. Where venv stands for the name of the virtual environment directory.
I was able to set directory as root (and fix the unresolved import issue) doing the following:
from pycharm>Settings>Project>Project Structure select your project and from the file tree, select your django project directory then click the blue folder Source button to define the directory as your source.

Eclipse - Pydev, doesn't recognize not imported methods

I have Eclipse Version: 4.2.1 and PyDev version 2.7.1 installed on my 64-bit Ubuntu.
I am using Python 2.7 and I have this problem with Eclipse that it doesn't recognize my un-imported methods. For example if I write a code like this:
def main():
myfiles = os.listdir('src')
if __name__ == '__main__':
main()
print'done!'
I get this error:
Traceback (most recent call last):
File "tset.py", line 5, in <module>
main()
File "tset.py", line 2, in main
myfiles = os.listdir('src')
NameError: global name 'os' is not defined
which is a pretty obvious error because I didn't write "import os" at the beginning of my code. My problem is that Eclipse doesn't highlight these errors for me anymore. Either I have to find them by myself or I will find out about them when I run my code.
My Eclipse was working fine before but I don't know what did I change that this happened.
I should also mention that I have "lib" folder in my project and in that folder I have a few of my own modules and I have added the "lib" folder to PYTHONPATH of my project. And the code that I am running is in another folder named "test" and that's not in the PYTHONPATH.
The problem was that my code was in a folder like this:
/MyProject/src/test/test.py
But the "src" folder was not in the PYTHONPATH. That is why Eclipse was not recognizing them as a source code and was not analyzing them.
To fix this go to:
Eclipse -> Project -> Properties -> PyDev-PYTHONPATH -> Source Folders
and add "/MyProject/src" to source folders. (I only had "/MyProject/src/lib" in my source folders)
In addition to the answer by #183.amir, if one of your apps is symlinked (not an actual directory), you need to add it to PYTHONPATH separately (with the steps described above).

Categories