Module import error only when run from command line - python

I can successfully run a python script from the IDE (PyCharm) but when I attempt to run this from the command line I get ModuleNotFoundError.
The module is PythonGit and is imported as follows after doing pip install:
from git import Repo
if __name__ == '__main__':
print('Imported git package')
I have a virtual environment set up as .venv/ and the package is present here: .venv/lib/python3.10/site-packages/git.
The output of pip list is as follows:
Package Version
------------------ -------
...
gitdb 4.0.9
GitPython 3.1.29
...
The virtual environment is activated when I attempt to run from the command line yet this is the error I am getting:
File "/Users/paul/Repos/dependencies/main.py", line 1, in <module>
from git import Repo
ModuleNotFoundError: No module named 'git'
There are no special settings in the run configuration in the IDE, the script path points to the same file (main.py) and the interpreter to the one in the virtual environment.
I have not been able to find any answer on these forums that successfully addresses this issue yet.

Try to install using setup.py from this https://github.com/gitpython-developers/GitPython
Just clone from git and run setup.py in ur directory

I just tested and successfully ran the python program.
Here are the steps I took:
Open a command prompt
Navigate to the folder that will host the virtual environment. In my case this was:
D:\Personal\Programming>_
Checked Python version using: py --version
D:\Personal\Programming>py --version
Python 3.10.8
Upgraded pip using: py -m pip install --upgrade pip
Checked pip version using: py -m pip --version
D:\Personal\Programming>py -m pip --version
pip 22.3.1 from ...\AppData\Local\Programs\Python\Python310\lib\site-packages\pip (python 3.10)
Ensured I had the latest virtualenv : py -m pip install --user virtualenv
Create a new virtual environment (env) using : py -m venv env
Activated the environment (env) using: .\env\Scripts\activate
The cursor now shows you are in the virtual enviroment:
(env) D:\Personal\Programming>_
Also you should see a new folder called env in the D:\Personal\Programming folder (or where ever you created the virtual environment.
Checked that python existed within the virtual environment: where python
(env) D:\Personal\Programming>where python
D:\Personal\Programming\env\Scripts\python.exe
Installed GitPython using: py -m pip install GitPython
(env) D:\Personal\Programming>py -m pip install GitPython
Collecting GitPython
Using cached GitPython-3.1.29-py3-none-any.whl (182 kB)
Collecting gitdb<5,>=4.0.1
Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)
---------------------------------------- 62.7/62.7 kB 3.5 MB/s eta 0:00:00
Collecting smmap<6,>=3.0.1
Using cached smmap-5.0.0-py3-none-any.whl (24 kB)
Installing collected packages: smmap, gitdb, GitPython
Successfully installed GitPython-3.1.29 gitdb-4.0.10 smmap-5.0.0
Checked pip list using: pip list
(env) D:\Personal\Programming>pip list
Package Version
---------- -------
gitdb 4.0.10
GitPython 3.1.29
pip 22.2.2
setuptools 63.2.0
smmap 5.0.0
Created the main.py program in my D:\Personal\Programming folder
Ran the main.py program: py main.py
(env) D:\Personal\Programming>py main.py
Imported git package
This shows that the program ran successfully.

Related

ModuleNotFoundError in Juypter notebook after installing local Python package

I tried to install my project as a package so I can import functions from my src folder. In my conda env in the root folder of my project in the terminal I tried pip install -e ./ and (after uninstalling my first attempt) python -m pip install -e ./, both of which resulted in
Installing collected packages: gliders
Running setup.py develop for gliders
Successfully installed gliders
and the package showing up when I did conda list
as
Name Version Build Channel
gliders 0.1.0 dev_0 <develop>
However when I run code with import gliders in my Jupyter notebook, I get the following error: ModuleNotFoundError: No module named 'gliders'
I am using Python 3.6.13.
How can I fix this error?

pip - No module named 'pip' even after successful installation

I am trying to install pip on my Windows 10 system. I got the get-pip.py file and ran the command
python get-pip.py
Here's a snapshot of the terminal
$ python get-pip.py
Collecting pip
Using cached
https://files.pythonhosted.org/packages/46/dc/7fd5df840e
fb3e56c8b4f768793a237ec4ee59891959d6a215d63f727023/pip-19.0.1-py2
.py3-none-any.whl
Collecting setuptools
Using cached
https://files.pythonhosted.org/packages/ff/47/1dfa4795e
24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-
none-any.whl
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-19.0.1 setuptools-40.8.0 wheel-0.32.3
Now when I try to check the version with
pip -V
I get this -
Traceback (most recent call last):
File "runpy.py", line 193, in _run_module_as_main
File "runpy.py", line 85, in _run_code
File "C:\Program Files\Python\python-3.6.3-embed-
amd64\Scripts\pip.exe\__main__.py", line 5, in <module>
ModuleNotFoundError: No module named 'pip'
Why is this happening? It JUST said pip installed successfully and when I try to see the version to check if its installed, it says No module named 'pip'
Also, I have included C:/Program/Files/Python/python3/Scripts in the PATH variable.
Please help ! ! ! !
After running python get-pip.py, python install-dir will increase dir Lib\site-packages
Method 1. try to cp the pip dir into the python install-dir
or
Method 2. change file python3x._pth in python install-dir,append this line Lib\site-packages
run pip, problem will be solved
Open python37._pth file which is in the root folder of python.
Add Lib\site-packages line.
All will work fine.
The following steps may be followed to install pip on windows:
Download python file: get-pip.py (nearly 1.8 MB size) from link: https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py and save it to your desktop
Go to command prompt and Check your python version: c:> python
From command prompt run: c:> python get-pip.py
The above will install new version of pip. check by running C:> pip --version
You may also update setup tools - C:> pip install --upgrade pip setuptools
I suggest referring to the pip homepage: https://pip.pypa.io/en/stable/installation/
It describes all methods on how to install pip.
I am a win10 user so the solution was to run this command:
py -m ensurepip --upgrade
Use following commands can be used to find out whether pip extraction path is included or not.
>>> import sys
>>> sys.path
if Lib\site-packages path is not included then update file python37._pth.
Run the command again and path should be visible.
pip install <module name> worked successfully for me after this.
Thanks #demianzhang for the hint or even for the solution.
I found files in my Windows lib\site-packages directory
~ip
~ip-20-0.2.dist-info
Renamed them to
pip
pip-20-0.2.dist-info
And it worked to run pip install --upgrade pip
If your Python environment does not have pip installed, there are 2
mechanisms to install pip supported directly by pip’s maintainers:
ensurepip
get-pip.py
Method 1
ensurepip
Python comes with an ensurepip module1, which can install
pip in a Python environment.
Linux: python -m ensurepip --upgrade
MacOc: python -m ensurepip --upgrade
Windows: py -m ensurepip --upgrade
Method 2
get-pip.py This is a Python script that uses some bootstrapping logic
to install pip.
Download the script, from https://bootstrap.pypa.io/get-pip.py.
Open a terminal/command prompt, cd to the folder containing the
get-pip.py file and run:
Linux: python get-pip.py
MacOc: python get-pip.py
Windows: py get-pip.py
Above installation commands are in official python link.
Optional
After successful installation of pip you might want to upgrade pip to latest version
for Linux and MacOs python -m pip install --upgrade pip
for Windows : py -m pip install --upgrade pip
py -m ensurepip --upgrade
this is work for me official docs link you can try multiple methods based on os.
https://pip.pypa.io/en/stable/installation/
I solved my problem by opening my shell as admin

azure webjob python error: could not create 'D:\Python34\Lib\site-packages\requests': Access is denied

I am trying to install requests from my requirements.txt file on Azure.
When I run:
python.exe -m pip install --upgrade -r requirements.txt
I get the error:
could not create 'D:\Python34\Lib\site-packages\requests': Access is denied
In my Azure webapp I have installed the following python extension:
Python 3.6.1 x64 3.6.1.3
So I am presuming that I need the 'requests' package to install to here:
D:\home\python361x64\Lib\site-packages\ instead of 'D:\Python34\Lib\site-packages\
Any ideas how I can fix this ?
Got it working by creating a run.cmd file & then specifying the python path to run python.exe from eg. D:\home\python361x64\python.exe get_stats.py – Pete Stilgoe yesterday
And running python.exe -m pip install --upgrade -r requirements.txt from the version path eg D:\home\python361x64\python.exe -m pip install --upgrade -r requirements.txt – Pete Stilgoe yesterday

pip installing to wrong folder even though `which pip` is correct

I'm using Mac OS X 10.10. I want to use pip to install packages for my homebrew installed version of python (located in /usr/local/bin/python, which is an alias that points to /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin). It appears that site-packages for this version are here: /usr/local/lib/python2.7/site-packages/.
which python returns /usr/local/bin/python
which pip returns /usr/local/bin/pip
These seem correct to me.
Trying something like pip install pylzma returns:
Collecting pylzma
Installing collected packages: pylzma
Successfully installed pylzma
You are using pip version 8.0.2, however version 8.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
But then pip list does not show pylzma to be installed. It looks like pip installs the packages to /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (the python that ships with Mac OS X).
How can I get pip to install to my homebrewed python?
I've tried a number of suggestions from similar questions:
I've tried export PATH=/usr/local/bin/python:${PATH}.
I've tried pip install --install-option="--prefix=/usr/local/lib/python2.7" pylzma.
I've tried changing the first line of the pip executable script to #!/usr/local/bin/python
I've tried /usr/local/bin/python -m pip install pylzma.
But none of these work. I also tried upgrading pip to 8.1.1, but that made pip break entirely. People recommend using virtualenv, but as far as I know, I can't install that without pip.
When I type python -m pip, it says:
Usage:
/usr/local/opt/python/bin/python2.7 -m pip <command> [options]
Could that be a problem?
My issue was that my /Users/<username>/.pydistutils.cfg contained the following:
[easy_install]
# set the default location to install packages
install_dir = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
[install]
install_lib = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
install_scripts = ~/bin
I changed this to:
[easy_install]
# set the default location to install packages
install_dir = /usr/local/lib/python2.7/site-packages
[install]
install_lib = /usr/local/lib/python2.7/site-packages
install_scripts = ~/bin
That seemed to have worked. pip install now installs packages to the desired location /usr/local/lib/python2.7/site-packages.
However, I am have ongoing path issues.
import pylzma still gives me ImportError: No module named pylzma.
and running jupyter notebook in terminal gives -bash: jupyter: command not found. /Users/<username>/bin/jupyter notebook does execute, but I get ImportError: No module named markupsafe despite the fact that /usr/local/lib/python2.7/site-packages/MarkupSafe-0.23.dist-info exists.
EDIT: I got jupyter notebook working eventually. I had to install several packages from the source tarballs directly, including MarkupSafe, functools32, and jsonschema. Maybe Python is not looking in the correct folder or something.

Unable to install a specific version of django on virtualenv

I have been trying to install django 1.8 on virtualenv, i performed the following steps:
changed to my project directory
changed to scripts folder of virtual environment which I created
activated the virtual env
typed command: pip install django == 1.8
Nothing worked
also, tried pip install django and easy_install django, however, none worked.
Could you please help me out ?
Can you try this command:
sudo pip install django==1.8
One problem could be that the command line doesn't tokenize the command correctly. Try the following (with the quotes around the requirement specifier):
pip install "django==1.8"
Like you haven't shared the output error, I suggest some actions:
Try to use the absolute path or add the path of your pip installation to your PATH system variable:
Go to CMD change the python instalation directory C:\>cd \Python27\Scripts and type C:\Python27\Scripts>pip -h
If works well you can use the absolute path C:\Python27\Scripts>pip install "django==1.8" or add the path of your pip installation to your PATH variable using the command above:
C:\Python27\Scripts>setx PATH "%PATH%;C:\Python27\Scripts"
SUCCESS: Specified value was saved.
Another attempt is updating pip scripts using easy_install
(FlaskEnv27) E:\Projects\FlaskEnv27\Scripts>easy_install pip
Searching for pip
Best match: pip 1.5.6
Removing pip 1.3.1 from easy-install.pth file
Adding pip 1.5.6 to easy-install.pth file
Installing pip-script.py script to E:\Projects\FlaskEnv27\Scripts
Installing pip.exe script to E:\Projects\FlaskEnv27\Scripts
Installing pip.exe.manifest script to E:\Projects\FlaskEnv27\Scripts
Installing pip3.4-script.py script to E:\Projects\FlaskEnv27\Scripts
Installing pip3.4.exe script to E:\Projects\FlaskEnv27\Scripts
Installing pip3.4.exe.manifest script to E:\Projects\FlaskEnv27\Scripts
Installing pip3-script.py script to E:\Projects\FlaskEnv27\Scripts
Installing pip3.exe script to E:\Projects\FlaskEnv27\Scripts
Installing pip3.exe.manifest script to E:\Projects\FlaskEnv27\Scripts
Using c:\python27\lib\site-packages
Processing dependencies for pip
Finished processing dependencies for pip

Categories

Resources