How to install and use python module on Win 64? - python

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

Related

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 fix 'no module named requests'

I have requests module installed but it shows an error when running .py file from cmd prompt.
There are no errors when running the file from vscode.
It seems that requests is installed in your virtual enviroment "my_env" but you must execute your script using the intepreter of your virtual enviroment (It seems that windows keep using the base interpreter even if you activated the virtual env). Try calling it directly, for example
"Your_path_to_venv\Scripts\python.exe" main.py
This issue can be caused by multiple issues.
Wrong pip version is in path (Caused by installing a newer version of python, while an older version was already there.
This can be fixed by simply removing the old python from PATH and restarting the command line.
Try using pip3 instead of pip maybe pip installs libraries in a different dictionary than pip3 does
If that didn't work then use
python -m pip install “your library”
And if you were using python3 just do the same
python3 -m pip install ”your library”
If they did not work too, replace pip in the last two commands with pip3
And if it still does not work first see the path of the python site-packages files by running this python code
import os
import inspect
print(os.path.dirname(inspect.getfile(inspect))+"/site-packages")
a path will be printed, take it and add it as a parameter to your pip command
pip install -t “the printed path”
Ok, I see that you probably have a script called requests.py in your C:\Users\BokaBu>pyproj\my_env\Scripts\activate folder, but your question is how can C:\Users\BokaBu>pyproj\src\main.py find it.
The solution is to add C:\Users\BokaBu>pyproj\my_env\Scripts\activate to PYTHONPATH. The solution to the general version of your question can be found here:
Importing files from different folder - Stackoverflow
But in more detail, you may want to try this to your C:\Users\BokaBu>pyproj\src\main.py:
# In C:\Users\BokaBu>pyproj\src\main.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'C:\Users\BokaBu>pyproj\my_env\Scripts\activate')
import requests
Hope it helps~

Failed to call module in python using cloud9 [duplicate]

Using sqlobject. When I import the module I get a unable to load module error when running lambda local or remote. The module is installed and if I get a command line python3 interpreter and import the module it imports just fine.
How do I get 3rd party modules installed so they work with both lambda local and lambda remote?
Code could not be simpler:
import sqlobject
Answering my own question...
These are instructions for Python 3.
First start with an empty environment, mine was called cycles.
Create a new lambda function:
Your folder structure now looks like this:
There will be two folders with the same name (a bit confusing - ask AWS not me).
Right button click on the top most folder with your lambda function name and select "Open terminal here". This gets you command line.
No need to use sudo, just install the packages you need. Install your packages into that folder:
python3 -m pip install --target=./ sqlobject
IMPORTANT
You need to install the packages in that top folder that you opens a terminal from. See the part of the pip install line that says:
--target=./
that makes sure the packages get installed in the right folder that lambda can use. If you use the standard pip install:
python3 -m pip install sqlobject
You packages will be installed in the wrong place.
Your folder structure should look like this with the new added packeges installed:
You can see the code to the right...it ran fine with the sqlobject package installed.

Automatic python code formatting in sublime

I am trying to find some package that would auto format python code when using sublime.
There is PythonTidy, but when I use PackageController it says install completed but the package is not installed (does not appear in preferences).
I did try following the instructions in:
https://github.com/witsch/SublimePythonTidy
and while i "pip installed" the package in python, sublime would not load, throwing:
terminate called after throwing an instance of 'boost::python::error_already_set'
/usr/bin/subl: line 3: 12415 Aborted
/usr/lib/sublime-text-2/sublime_text --class=sublime-text-2 "$#"
How would I go about installing this without PackageController, or alternatively, can anyone recommend another package?
ctrl+shift+P then Package Control: Install Package
Look for Python PEP8 Autoformat and install it.
Try doing the following in command line (a bit brute force):
Navigate into the Packages/PythonTidy folder,
usually ~/.config/sublime-text-2/Packages/PythonTidy
or ~/.config/sublime-text-2/Packages/SublimePythonTidy
If it's non-existent reinstalling using Package Control
Inside there should be another PythonTidy folder (which in your case will be empty).
Don't get into it, just check it is empty.
Run git clone https://github.com/witsch/PythonTidy.git
Restart sublime and check the console for errors (View -> Show Console)
P.S.
If you are unable to start Sublime do a:
sudo pip uninstall PythonTidy
Then retry what I wrote above.
Package Control should create an appropriately named folder in Sublime Text's packages folder. You can get there from Preferences > Browse Packages. If the package installed correctly, there should be a folder called PythonTidy. If there's not, you can download the package from github directly and place the folder in this Packages folder.

How to install python modules in blender

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

Categories