How to make python update module install dir after moving folder? - python

I installed a Python module from GitHub. Here is an example of the commands I ran:
cd ~/modules/
git clone https://github.com/user/xyz
cd xyz
python setup.py develop
this installed the the module succesfully in the current folder. Then from some other folder, I did:
cd ~/test
python -c 'import inspect; import xyz; print(inspect.getfile(xyz))'
wich gave the following output:
/home/hakon/modules/xyz/xyz/__init__.py
Now, I decided I wanted to move the install folder. For example,
cd ~/modules/xyz
mv xyz xyz2
But now Python cannot find the module any longer. For example:
cd ~/test
python -c 'import inspect; import xyz; print(inspect.getfile(xyz))'
With output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'xyz'
Questions:
Where does Python register the location of modules?
How can I update the registry to the new location of the module?

According to the documentation for setup tools develop command:
The develop command works by creating an .egg-link file (named for the
project) in the given staging area. If the staging area is Python’s
site-packages directory, it also updates an easy-install.pth file so
that the project is on sys.path by default for all programs run using
that Python installation.
So to answer the question to update the location after you have moved the project folder you can rerun the develop command (from the new folder):
python setup.py develop
Note: you could also unregister the project before you moved it:
python setup.py develop --unistall
but this is not necessary if you just want to update the location.

Related

How to create an .exe with a batch file from a project that has Pyqt6

I have a project with multiple scripts and I want to transform this project to an *.exe file. I've wrote a *.bat file to build the *.exe:
echo off
pip install virtualenv
virtualenv venv_build
call ./venv_build/Scripts/activate
pip install -r requirements.txt
pyinstaller main.py -F
deactivate
rmdir /S /Q venv_build
pause
requirements.txt:
pandas~=1.5.1
numpy~=1.23.5
matplotlib~=3.6.2
tensorflow-gpu~=2.10.1
openpyxl~=3.0.10
PyQt6~=6.4.0
ipython~=8.6.0
keras~=2.10.0
scikit-learn~=1.1.3
scipy~=1.9.3
It allows me build my virtual venv and install my requirements and create an *.exe file. However, I reveive that error when I tried to open the *.exe :
Traceback (most recent call last):
File [myfile_path], line 5, in <module>
from PyQt6.Qtwidgets import QApplication
ModuleNotFoundError: No module name 'PyQt6'
I've seen that I can write pyinstaller -F --hidden-import PyQt6 main.py to do an hidden install of PyQt6. It still doesn't work.
It also doesn't make sense to me to do an hidden-import, because that library is in the requirements.
So If someone has an idea ?
Other questions :
My project has mutiple scripts, when I use pyinstaller main.py , does it transform also the other scripts ?
I have images for some buttons in my GUI, they are created like that : btn_load_tests = QPushButton(QIcon("Icons/folder-import.png"), ""). Are those links gonna be broken ?
Same thing for th Icon of my application : self.setWindowIcon(QIcon("Icons/deep-learning.png")) [self being a QMainWindow]

Issue with Python 3 Imports when using a Shell script

I have the following scenario:
A Python3 Package (meross_iot) installed through pip and located in
~/.local/lib/python3.7/site-packages
A Python script (meross_electricity.py) that imports from this package: from meross_iot.controller.mixins.electricity import ElectricityMixin
A shell script (launcher.sh) that is meant to be a wrapper so that the .py script is run at startup:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/Documents
sudo python meross_electricity.py
cd /
If I simply execute the .py file everything works as expected, imports done, etc. If I try to run the .sh script I get the following error:
pi#home:~/Documents $ ./launcher.sh
Traceback (most recent call last):
File "meross_electricity.py", line 4, in <module>
from meross_iot.controller.mixins.electricity import ElectricityMixin
ModuleNotFoundError: No module named 'meross_iot'
Can someone please help me solve this issue?
Thanks!
Installed package with sudo worked .
Answer based on comment by #KlausD

pdfminer - ImportError: No module named pdfminer.pdfdocument

I am trying to install pdfMiner to work with CollectiveAccess. My host (pair.com) has given me the following information to help in this quest:
When compiling, it will likely be necessary to instruct the
installation to use your account space above, and not try to install
into the operating system directories. Typically, using "--
home=/usr/home/username/pdfminer" at the end of the install command
should allow for that.
I followed this instruction when trying to install.
The result was:
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/home/username/pdfminer/bin/latin2ascii.py to 755
changing mode of /usr/home/username/pdfminer/bin/pdf2txt.py to 755
changing mode of /usr/home/username/pdfminer/bin/dumppdf.py to 755
running install_egg_info
Removing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info
Writing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info
I don't see anything wrong with that (I'm very new to python), but when I try to run the sample command $ pdf2txt.py samples/simple1.pdf I get this error:
Traceback (most recent call last): File "pdf2txt.py", line 3, in <module>
from pdfminer.pdfdocument import PDFDocument ImportError: No module named pdfminer.pdfdocument
I'm running python 2.7.3. I can't install from root (shared hosting). The most recent version of pdfminer, which is 2014/03/28.
I've seen some posts on similar issues ("no module named. . . " but nothing exactly the same. The proposed solutions either don't help (such as installing with sudo - not an option; specifying the path for python (which doesn't seem to be the issue), etc.).
Or is this a question for my host? (i.e., something amiss or different about their setup)
I had an error like this:
No module named 'pdfminer.pdfinterp'; 'pdfminer' is not a package
My problem was that I had named my script pdfminer.py which for the reasons that I don't know, Python took it for the original pdfminer package files and tried to compiled it.
I renamed my script to something else, deleted all the *.pyc file and __pycache__ directory and my problem was solved.
use this command worked for me and removed the error
pip install pdfminer.six
Since the package pdfminer is installed to a non-standard/non-default location, Python won't be be able to find it. In order to use it, you will need to add it to your 'pythonpath'. Three ways:
At run time, put this in your script pdf2txt.py:
import sys
# if there are no conflicting packages in the default Python Libs =>
sys.path.append("/usr/home/username/pdfminer")
or
import sys
# to always use your package lib before the system's =>
sys.path.insert(1, "/usr/home/username/pdfminer")
Note: The install path specified with --home is used as the Lib for all packages which you might want to install, not just this one. You should delete that folder and re-install with --
home=/usr/home/username/myPyLibs (or any generic name) so that when you install other packages with that install path, you would only need the one path to add to your local Lib to be able to import them:
import sys
sys.path.insert(1, "/usr/home/username/myPyLibs")
Add it to PYTHONPATH before executing your script:
export PYTHONPATH="${PYTHONPATH}:/usr/home/username/myPyLibs"
And then put that in your ~/.bashrc file (/usr/home/username/.bashrc) or .profile as applicable. This may not work for programs which are not executed from the console.
Create a VirtualEnv and install the packages you need to that.
I have a virtual environment and I had to activate it before I did a pip3 install to have the venv see it.
source ~/venv/bin/activate

Cannot import distutils from within a py2exe-compiled script

I am on Windows Server 2012R2, trying to compile a script with py2exe, within a virtualenv, and I'm getting issues whenever one of the application scripts tries to "import distutils" (in my case, it's somewhere inside a 3rd-party library, but I reduced the problem here).
Steps to reproduce:
Create a virtualenv
virtualenv venv
call venv\Scripts\activate
Install py2exe inside the virtualenv
easy_install --always-unzip py2exe-0.6.9.win64-py2.7.amd64.exe
Create setup.py
from distutils.core import setup
try:
import py2exe
except:
pass
setup(
console=[
'py2exe_distutils.py'
]
)
Create py2exe_distutils.py
import distutils
Run py2exe
python setup.py py2exe
Try to run the generated executable
dist\py2exe_distutils.exe
It returns:
C:\Users\root\p\dist\library.zip\distutils\__init__.py:14: UserWarning: The virtualenv distutils package at %s appears to be in the same location as the system distutils?
Traceback (most recent call last):
File "py2exe_distutils.py", line 6, in <module>
import distutils
File "distutils\__init__.pyc", line 25, in <module>
ImportError: cannot import name dist
The script runs fine when I run it directly (python py2exe_distutils.py), even from within the virtualenv.
Am I trying to do something unsupported by py2exe, or is something wrong with my setup?
I had the same problem while creating an executable that used pandas 0.12.0. This worked for me: before you create the executable, copy the distutils folder from the base python installation
robocopy C:\Python27\Lib\distutils venv\Lib\distutils /E /COPY:DAT
I am using virtualenv 12.0.4 and py2exe 0.6.6 on Windows 7 Professional. Some extra insight can be found here. This answer pointed me in the direction of just copying the files.

How should I handle software packages?

I am trying to install pysqlite and have troubles with that. I found out that the most probable reason of that is missing sqlite headers and I have to install them. My platform: CentOS release 5.3 (Final). I have Python-2.6.2.
I also found out that I need .rpm files. As far as I have them I execute:
rpm -i sqlite3-devel-3.n.n.n.rpm
and everything should be fine.
However, I do not know where to find sqlite3-devel-3.n.n.n.rpm file. Should it already be on my system? I could not locate it with "locate sqlite3-devel-3". Should I download this file? If yes where I can find it and which version should I use? I mean, the .rpm file should be, probably, consistent with the version of sqlite that I have on my computer? If it is the case, how can I find out the version of my sqlite?
If I type "from pysqlite2 import dbapi2 as sqlite" I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pysqlite2
"yum search pysqlite" gives me the following:
Loaded plugins: fastestmirror
Excluding Packages in global exclude
list Finished
==== Matched: pysqlite ==== python-sqlite.x86_64 : Python bindings
for sqlite.
By the way, I have the following directory:
/home/myname/opt/lib/python2.6/sqlite3
and there I have the following files:
dbapi2.py dbapi2.pyc dbapi2.pyo
dump.py dump.pyc dump.pyo
__init__.py __init__.pyc __init__.pyo test
If I type "import unittest" and then "import sqlite3 as sqlite" I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/myname/opt/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import * File "/home/myname/opt/lib/python2.6/sqlite3/dbapi2.py",
line 27, in <module>
from _sqlite3 import * ImportError: No module named _sqlite3
Thank you in advance.
Python 2.6 (and some earlier) include sqlite Python org library ref so you should not need to do this. Just import it and run
You can use buildout to create localized version of your project. This will install all necessary packages without having sudo access to the server.
To give it try, do the following:
mkdir tmp
cd tmp
wget http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py
python bootstrap.py init
vim buildout.cfg
edit buildout.cfg and replace it with following:
[buildout]
parts = sqlite
[sqlite]
recipe = zc.recipe.egg
eggs = pysqlite
interpreter = mypython
Now, run ./bin/buildout to rebuild the project. This will download all of the necessary packages and create a new interpreter for you that you can use test that you can access sqlite.
./bin/buildout
./bin/mypython
>>> import sqlite3
This gives you a controlled environment that you can use to develop inside of.
To learn more about buildout, you can watch videos from pycon 2009 on Setuptools, Distutils and Buildout.
Eggs and Buildout Deployment in Python - Part 1
Eggs and Buildout Deployment in Python - Part 2
Eggs and Buildout Deployment in Python - Part 3
Good luck
Typically, you should install the python sqlite module through yum, something like:
yum install python-sqlite
and then edit your code changing sqlite2 references to sqlite3.
By the way, whenever you read directions to install sqlite3-devel-3.n.n.n.rpm, the n parts are not literal; they're supposed to be replaced with numbers specifying a version of the rpm package.

Categories

Resources