Importing modules with python - python

I'm working with python 2.7 (32-bit) on Windows Vista. I downloaded some libraries including numpy, scipy, and pygame. When I try to import these modules the output says
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named numpy
I can find these modules in my python folders but for some reason it doesn't want to recognize it or some thing. (I made sure the file were compatible before downloading.)

Each package should contain a setup.py file. Run this with the command python setup.py install
For more information:
http://www.scipy.org/Installing_SciPy/Windows
http://pygame.org/install.html

"Everytime I run the command "python setup.py install" in the python shell it says SyntaxError: invalid syntax "
Please create a folder where you unzip the "numpy.zip" file, say the folder name is "c:\numpy"
Open a MS-DOS terminal (as described here : http://en.wikipedia.org/wiki/Command-line_interface), and type :
cd c:\numpy
python setup.py config
python setup.py install
This will set up the needed files in the correct folders for your python.
Enjoy !

To properly install this module:
In the Windows search bar (START MENU) type Command Prompt
Right-click on the Command Prompt icon (DO NOT go to cmd)
Click Run as Administrator
in the COMMAND PROMPT (DO NOT open cmd) type:
pip install numpy
This will install that module.
NOTE: If you are NOT in admin mode, you will get a message saying a fatal error has been raised.

Related

How do I run a setup.py file without being in its directory?

To install a package that includes a setup.py file, usually you cd into the root directory where setup.py is located and then run the following:
python setup.py install
Now my problem is that I'm building an addon for another application, and while I can install "normal" package via code with the following:
subprocess.run([sys.executable, "-m", "pip", "install", package])
I also have to install a couple custom made packages, and if I try to do it like this:
subprocess.run([sys.executable, "-m", "/MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
I get the following error: Error while finding module specification for 'MSN-Point-Cloud-Completion-master/emd/setup.py' (ModuleNotFoundError: No module named 'MSN-Point-Cloud-Completion-master/emd/setup')
What is annoying me is that if I manually cd into the directory and simply run python setup.py install from the cmd it works just fine.
So the problem seems to be passing the relative path to the setup.py file, but as I said, considering this is an addon I need to install that setup.py from code. Any solution?
Reading comments made me realize a few mistakes.
First of all I realized that I shouldn't use -m flag in this case and this allow me to execute setup.py correctly. Also deleted the extra slash in the path and now the following works:
subprocess.run([sys.executable, "MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
However, since I wasn't in that directory still, I also got the following error c1xx: fatal error C1083: It is not possible to open file: 'emd_cuda.cu': No such file or directory and the compilation failed.
This definitely fixed all the problems:
import os
import subprocess
os.chdir("MSN-Point-Cloud-Completion-master/emd/")
subprocess.run([sys.executable, "setup.py", "install"])

ImportError: No module named pygments.styles when attempting to run Pythontex

When I try to run pythontex via my command line((base) Zachs-MacBook-Pro:mat_300 zachmaurus$ pythontex pythontex.tex) the following occurs:
Traceback (most recent call last):
File "/Library/TeX/texbin/pythontex", line 50, in <module>
import pythontex2 as pythontex
File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex2.py", line 61, in <module>
from pygments.styles import get_all_styles
ImportError: No module named pygments.styles
How do I go about solving this issue? I have downloaded pygments with pip install, but for whatever reason when I attempt to run the script the module pygments.style cannot be found.
Solved this issue by adding pythontex_install.py to my PATH using export PATH=$PATH:/usr/zachmaurus/Downloads/pythontex/pythontex_install.py in MacOS Terminal. To ensure that pythontex_install.py has been added to PATH environment run echo $PATH and it should appear as one of the variables. After having added pythontex_install.py to PATH, I now get the following result when I run pythontex pythontex.tex to compile my document:
This is PythonTeX 0.16
--------------------------------------------------
PythonTeX: pythontex - 0 error(s), 0 warning(s)
I solved this problem by explicitly call the interpreter I want, wenn I compile the document.
pdflatex -interaction=nonstopmode document.tex
python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
or for any python interpreter you want:
pdflatex -interaction=nonstopmode document.tex
<path_to_python>/python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
Of course Pygments module schuld be installed by this interpreter. This worked very good for me on macOS.
I got the same problem when I tried to use pythontex on my macbook (on macOS Catalina 10.15).
What I suggest below must be (if used) used with precaution :
I tried so many solutions and what actually worked for me was this :
I made sure that pygments was installed on my machine (I ran 'pip install pygments' on the terminal)
Then I changed the first line of the files pythontex, pythontex2 (because the problem seemed to come from there) and pythontex3
Theses files (if you are using macOS Catalina 10.15 like me) should be in
/usr/local/texlive/2021/texmf-dist/scripts/pythontex
(if you installed TexLive2021) -> you should be able to retrieve this path when you try to compile your tex file anyway...
The first line was replaced by #!/usr/bin/python3 (to ensure that python3 was used instead of python 2.7 which is used by default by macOS) inside the 3 files pythontex, pythontex2 and pythontex3 then :
I ALSO added this line (see the screenshot below) in pythontex3 :
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/')
to ensure that python found the module pygments
(because I installed python3.9 on my machine and the folder "pygments" and its subfolder "styles" was present in the folder "site-packages")
pythontex3 file
So now everything is working fine again and I can use pythontex =)

Pyinstaller create an executable with external libraries

I am trying to create an executable file (exe) for a Python script that I written in PyCharm.
When I run the script from the PyCharm is working fine but when I try to run it as an individual .py or try to run the exe I am getting an error.
The issue I believe is with the "from infi.devicemanager import DeviceManager" library.
from infi.devicemanager import DeviceManager # Used for the retrievement of Device manager information
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices # Get all the devices to a list called devs
for d in devs:
print (d.description)
I get following error:
PS C:\Python> python.exe .\tests.py
Traceback (most recent call last):
File ".\tests.py", line 1, in <module>
import infi.devicemanager # Used for the retrievement of Device manager information
ModuleNotFoundError: No module named 'infi'
PS C:\Python> pyinstaller -F .\tests.py
PS C:\Python\dist> .\tests.exe
Traceback (most recent call last):
File "tests.py", line 1, in <module>
ModuleNotFoundError: No module named 'infi'
[15072] Failed to execute script tests
Is there a way to include this library to the exe file so anyone without python can run this script?
I am open to suggestions.
Thank you in advance!
=== UPDATE ===
Full script can be found here
https://github.com/elessargr/k9-serial
My answer assumes that the interpreter that has infi.devicemanager is C:\Python\venv\Scripts\python.exe as stated by OP in the comments
So there is virtual environment named venv. You need to activate the virtual environment and install PyInstaller in it.
C:\Python>venv\Scripts\activate
(venv)C:\Python>
Note (venv) in front of your shell - that means virtual environment venv is activated
Now install PyInstaller
(venv)C:\Python>pip install pyinstaller
-- here it will install pyinstaller --
now you can test that both pyinstaller and infi.devicemanager are installed in venv
(venv)C:\Python>pip list
it should list both packages among some others. now
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager
--here it will create the exe --
if I was right it should work now
EDIT: It looks like infi is using __import__(pkg_resources), so the command should be
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager --hiden-import=pkg_resources

How to install additional packages in WinPython?

I have been using WinPython 2.2.5 with Python 2.7 and it works nice. The problem that I have is when I want to install additional libraries to use from the https://pypi.python.org repository.
For example I tried to install pdfminer which is in following link: https://pypi.python.org/pypi/pdfminer/
I have read that I can use pip install which is in the following path on my computer:
C:\WinPython-32bit-2.7.6.3\python-2.7.6\Scripts
On that directory I have saved the tar.gz file of pdfminer and from the windows command prompt on the aforementioned path I have typed:
pip install pdfminer(version number).tar.gz
It seems that it works fine, because there are no error messages, but when I open the winpython and in the command shell I put:
pdf2txt
to see if it works I got the following error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pdf2txt' is not defined
What am I doing wrong?
According to the documentation, "PDFMiner comes with two handy tools: pdf2txt.py and dumppdf.py." So, instead of trying to run pdf2txt.py by importing it, you need to run it as it shows in the example in the documentation, like this:
$ pdf2txt.py -o output.html samples/naacl06-shinyama.pdf
where output.html is the file that is created from the mined text, and samples/naac106-shinyama.pdf is the PDF you want to mine.

GYB (got your back) Python install on Mac Yosemite triggers py2exe error

I would like to backup the contents of my gmail account.
A recommended solution is Python module:
https://github.com/jay0lee/got-your-back/releases
But within its setup.py file there is a reference to Windows py2exe, which triggers this message when I run setup.py install:
File "setup.py", line 2, in &LT;module>
import py2exe, sys, os
ImportError: No module named py2exe
As mentioned in the installation on mac page, you don't have to run setup.py to use the software :
Now run:
python gyb.py --email youremail#gmail.com --action estimate

Categories

Resources