How to install python modules in blender - python

I've been trying to install pyserial for blender, but I can only install it to python32 on my C drive, is there anything i can do to have it install to blender or have blender import from python32

For windows, with no special permissions, and from blender python script only:
Install package you want from blender script (tqdm for example given below):
import pip
pip.main(['install', 'tqdm', '--user'])
From blender console watch the path where pip actually installs packages in your configuration (WARNING: The script tqdm.exe is installed in 'C:\Users\<Username>\AppData\Roaming\Python\Python39\Scripts' which is not on PATH):
In blender script add the path where your blender's pip installs packages to PATH:
import sys
packages_path = "C:\\Users\\<Username>\\AppData\\Roaming\\Python\\Python39\\Scripts" + "\\..\\site-packages"
sys.path.insert(0, packages_path )
Successfully import your package in the script:
import tqdm
Update 1
To show Blender terminal in v2.93 click Window -> Toggle System Console
Update 2
The whole script
# 1. launch next 2 lines of code in blender python interpreter
import pip
pip.main(['install', 'tqdm', '--user'])
import sys
# 2. watch blender's python path in console output at this moment
# 3. insert the path to packages_path below and uncomment
# packages_path = "C:\\Users\\<Username>\\AppData\\Roaming\\Python\\Python39\\Scripts" + "\\..\\site-packages" # the path you see in console
# 4. uncomment the next code and launch script in blender interpreter again
# sys.path.insert(0, packages_path )
# import tqdm
# use installed packages here

Blender has its own python installation and libraries. You can try to install your packages to blender directly. My dir for example: ...\Blender 2.63\2.63\scripts\modules
Otherwise, you can always hardcode the paths directly in your code with sys.path.append("...")
More info about installing modules available here, read about python setup.py install --home=<dir>

If you are on Windows you can just do python setup.py install as usual using the python interpreter given by blender. So for example, 'c:/Program Files/Blender Foundation/Blender/2.78/python/bin/python.exe' setup.py install.
On Linux, I think the native python3 interpreter is used so there is no problem of this kind.

Move the python file into a zip. Find the zip file from
Blender > Preferences > Add-on > Install
It should then show up in the list in the Add-on page.
Done.

make a permanent link of your python (3.5 and above needed) and replace your python directory in blender to directly use your systems python in blender...
U need to run cmd as administrator (use right click on the item)
D:\Blender Foundation\Blender\2.77>mv python python_old
D:\Blender Foundation\Blender\2.77>mklink /j python d:\Anaconda2\envs\py3
Junction created for python <<===>> d:\Anaconda2\envs\py3

After a lot of search and experiments, I have found this solution:
give all permissions to python folder in Blender installation
download get-pip.py and install it with the Blender's internal python executable
now you can install any modules by using the internal pip: ...\bin>python.exe -m pip install module_name
More details are described here: https://blender.stackexchange.com/questions/218486/installing-pythonnet-in-blender?noredirect=1#comment368756_218486

Related

Python module not found even though its installed

Does anyone else have this issue? For reference check this screenshot:
PIP is also up to date and I already tried reinstalling it but it didn't work neither. I'm using VS Code. It also worked just fine yesterday but today it didn't anymore for some reason.
Your mss package is installed locally, but you are using a virtual environment to run code.
Two solutions:
1. Switch the interpreter to the local interpreter, which is the c:\Users\Anwender\AppData\Local\.... in your picture
Ctrl+Shift+P to open the command palette, search for and select Python:Select Interpreter
Select the interpreter with the mss package installed
2. Install the mss package for your virtual environment
Select the virtual environment interpreter in the selection panel
Create a new terminal activation environment
Install mss package using command pip install mss
you can try installing mss outside of the venv and see if that works. also check that the venv you're using is the right one according to vscode
If you are using multiple versions of python or use any python version in appdata, without venv, you can try running "pip install mss" from elevated (administrator) cmd instance within python install directory/scripts, shown on the console image above. If it is not the right python version used in the project, same applies to any installed python version. If IDE is used check interpreter setting to determine which one is in play.
If you are using a virtual environment, you can install you package directly there. For windows:
py -m pip install mss
More reading
The other solution can be to add the path of your package to sys.path:
import sys
my_pkgs_path = r'/home/.../my_pkgs'
if my_pkgs_path in sys.path:
print('already in sys.path')
else:
print('not found in sys.path')
sys.path.append(my_pkgs_path)
print('Added to sys.path')

Import module could not be resolved

I've successfully installed "PyPDF2" module and checked it using the command 'pip list'.
yet when I try to import it i receive this message:
'''Import "PyPDF2" could not be resolved'''
The version of python that you installed the module in and the version that your IDE is using might be different check what version you installed it in by typing
pip -VV
in your terminal and check in what version your IDE is using (for vs code its usually shown at the bottom left or can be seen in the command pallete by typing Python: Select Interpreter)
another reason this could happen is if you are using an IDE like pycharm which by default creates a virtual environment for each project in which case you would need to either install your module from pycharm's terminal or activate the virtual environment and then install in it (docs here show how to https://docs.python.org/3/tutorial/venv.html)
Make sure you have installed PyPDF2
pip install PyPDF2
Select the correct interpreter
Click
First of all, please make sure that you are using the correct environment and python interpreter. If multiple python versions exist on your computer, use the command to install the PyPDF2 package under the version you need. For example python3:
py -3 -m pip install PyPDF2
Another possible reason: Paths are not imported correctly. Please add the following commands in the setting.json file:
"python.analysis.extraPaths":[
// The folder path where the custom module is located, and multiple paths can be added (the following is just an example)
// absolute path
"c:\\workspace\\pythontest\\.venv\\lib\\site-packages",
"E:\\myfolder\\homework\\one\\Person_reID_baseline_pytorch-master",
// relative path
"./src",
"./modules"
]

How to install and use python module on Win 64?

Trying to make use of this package: https://github.com/microsoft/Simplify-Docx
Can someone pls tell me the proper sequence of actions needed to install and use the package?
What I've tried (as a separate commands from vscode terminal):
pip install python-docx
Git clone <git link>
python setup.py install
After the installation has been successfully completed I'm trying to run from VS Code terminal the file in which I've pasted the code from readme's "usage" section:
import docx
from simplify_docx import simplify
# read in a document
my_doc = docx.Document("docxinaprojectfolder.docx") //I wonder how should I properly specify the path to file?
# coerce to JSON using the standard options
my_doc_as_json = simplify(my_doc)
# or with non-standard options
my_doc_as_json = simplify(my_doc,{"remove-leading-white-space":False})
And I only get
ModuleNotFoundError: No module named 'docx'
But I've installed this module in the first place.
What am I doing wrong? Am I missing some of the steps? (Like init or smth).
Vscode status bar at the bottom left says that I'm using python 3.8.x, and I'm trying to run the script via "play" button.
python --version
Python 3.6.5
py show's though that 3.8.x is being used.
Thanks
Amin sama was right - that was indeed an environment issue.
Looks like modules were getting globally installed in an older python folder. Different from the python which runs when you try to run python file. So I had to uninstall the older python.
After that
py --version
and
Python --version
Started to show the same version unlike before.
So, the sequence
1. Opened a fresh folder within VS Code
2. git clone <git link to repository from github>
3. copied all the files from cloned repo to my current folder (or you can go one level down with cd command)
4. installed dependency: pip install python-docx
5. run setup.py from where you copied files: python setup.py install
6. Copy "usage" into a new file, for example run.py
7. Specify an absolute path to your file with double backslash.
8. Add strings to run.py to output the result in a json:
import json
with open('data.txt', 'w') as f:
json.dump(my_doc_as_json, f, ensure_ascii=False)
Run this file from the terminal opened in your project folder typing run.py or python run.py
It wasn't necessary to open >>> python console.
The problem is that your system doesn't have "docx" module.
to install docx module you will have to install docx.
steps to install:
1) open CMD prompt.
2) type "pip install docx"
if your installation is fresh it may need "simplify" module too.
Like any python package that doesn't come with python, you need to install it before using it. In your terminal window you can install if from the Python package index like this:
pip install simplify-docx
or you can install it directly from GitHub like this:
pip install git+git://github.com/microsoft/Simplify-Docx.git

How to download and install Python Packages and Modules with Pip and those that can't be downloaded by pip?

I am trying to install module using pip to run some ML-based python programs. Can somebody share your inputs on this. I tried googling but didn't find a solution... Any help would be appreciated. Thanks.
PS :- I already installed pip.
You could go to the path where python is installed for example something like this:
>>cd c:/users/demongking/python3/scripts
>>c:/users/demongking/python3/scripts>pip install pandas
you could also go to http://www.lfd.uci.edu/~gohlke/pythonlibs/ and download the wheel file (.whl) and then go to your download directory and from there you could do something like this:
>>c:/users/demonking/downloads>>c:/users/demongking/python3/scripts/pip
install modulename.whl
Alternatively you could download setup.py file and then go to cmd and change the directory to where the setup file is downloaded and then type
>>python setup.py install
Make sure to write full name of python version if you have multiple versions installed
Alternatively you could also do this:
>>py -version -m pip install modulename
where -version is your python version
if you want to recognize pip as a command directly by cmd you would have to change the PATH system variable
>>In Search, search for and then select: System (Control Panel)
>>Click the Advanced system settings link.
>>Click Environment Variables. In the section System Variables, find the
PATH environment variable and select it. Click Edit. If the PATH
environment variable does not exist, click New.
>>In the Edit System Variable (or New System Variable) window, specify the
value of the PATH environment variable. Click OK. Close all remaining
windows by clicking OK.
Path would be where pip is contained.For example it would be something like this
c:/users/demongking/python3/scripts/pip
If the problem is installing packages that require compiling code, there are alternatives like:
anaconda - a python distribution with an alternative package manager
gohlke - a collection of prebuilt windows packages for many python modules

Wx can't run on mac

I installed anaconda instead of system's python on mac,but when I type
import wx
app = wx.App()
I got this:
This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.
I use the script:
#!/bin/bash
# what real Python executable to use
PYVER=2.7
PYTHON=/Library/Frameworks/Python.framework/Versions/$PYVER/bin/python$PYVER
# find the root of the virtualenv, it should be the parent of the dir this script is in
ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"`
# now run Python with the virtualenv set as Python's HOME
export PYTHONHOME=$ENV
exec $PYTHON "$#"
but it just used the system python.Can't use the lib in anaconda.
I want to use the wx GUI in anaconda,how to solve the problem?
I fixed the issue for my Python 2 and 3 virtual envs by reading this post (see my tip in the followed):
https://blurringexistence.net/wxpython-using-virtualenvwrapper-on-osx.html
My environment is:
macOS 10.12.5
Python 2 installed by Homebrew
wxPython installed through: brew insstall wxpython
Python 3 installed by Homebrew
wxPython installed through: gpip3 install wxpython (the global PIP for Python 3)
Tips:
You could modify the script in that post to work for Python 3.
You should set "PYTHONHOME" in the "activate" file instead of "postactivate" if you DO NOT use virtualenvwrapper.

Categories