How should git bash be set up to run an non pip installed python library?
I am trying to run a Python library that I have git cloned (not pip installed) on Windows using git bash. I keep getting an error that the module cannot be found even though I have set the $PYTHONPATH to point to this folder.
This occurs both when using a venv and the base environment.
I've activated the venv using the activate command in the venv folder, and then added the folder to the PYTHONPATH as is shown here (excuse the blanking):
The first line shows the error I get when running the script which calls in other modules. The second shows my PYTHONPATH, the third my PATH
Adding the local folder to the path is how this functionality works on mac.
This the particular file that is meant to run, datagen.py:
#!/usr/bin/env python
from msq_data_gen.cli import cli
if __name__ == '__main__':
cli.datagen()
The code fails on the first import of msq_data_gen.
The structure of the program is as so:
Curiously, the integrated running of Pycharm works correctly, but as the code is based off a CLI, this isn't the intended usecase. (I could also use the unit testing mode for click, but I think an answer for this could be useful for other people!)
I'm trying to debug some python code using VS code. I'm getting the following error about a module that I am sure is installed.
Exception has occurred: ModuleNotFoundError
No module named 'SimpleITK'
File "C:\Users\Mido\Desktop\ProstateX-project\src\01-preprocessing\03_resample_nifti.py", line 8, in <module>
import SimpleITK as sitk
I installed the module using
sudo pip install SimpleITK
I know that it was installed because I was getting a similar error when I ran the code through the command line, and it was fixed by doing the above. I don't understand why VS code does not recognize that
After installing a new module via pip reloading vscode may work if vscode doesn't recognize it. To do this, make sure that the module is installed inside the virtual environment by creating and activating a virtualenv:
python3 -m venv env
source env/bin/activate
Make sure to use the correct way of installing a module with pip:
python3 -m pip install {new_module}
Replace the string "{new_module}" with your module name. After that, make sure to reload vscode by clicking Ctrl+Shift+P, and selecting Reload window.
Now vscode will know the new module and autocompletion works.
sudo pip install is most likely installing globally into a Python interpreter that is different than the one that you have selected in VS Code. Please select the Python interpreter you want to use and then install explicitly using that interpreter (if you're not using a virtual environment then use something like /path/to/python -m pip install SimpleITK, although I strongly recommend using a virtual environment and to not install packages globally).
In Mac, correctly selecting the Python Interpreter worked for me:
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
No interpreter selected
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.
Source :VS Code Select Interpreter
This error: your vscode use other python version. This solution change vscode use current python.
In terminal find current python version:
py --version
In vscode Press Ctrl+Shift+P then type:
Python: Select Interpreter
Select current python version
I ran into this problem with VSCode and resolved it by setting my Python interpreter within VSCode to the same as the one in my system path (type "echo %PATH%" on Windows and look for Python) via the process here: https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter
There are a lot of proposed answers that suggest changing the launch.json or the settings.json file. However, neither of these solutions worked for me.
My situation:
Is Python environment selected? yes
Does the Terminal recognize Python environment? yes
Can I run the Python code from the activated Terminal? yes
Does the code run w/o error when I use "Start Debugging"? yes
Does the code run when I click "Run Code"? no
The only solution that worked for me is to:
Open Windows Terminal (or cmd)
Activate environment: conda activate <environment_name>
Open Visual Studio Code from Terminal: code
Then, "Run Code" (#5) works without any issues.
Source:
"module not found error" in VS Code using Conda - l3d00m's answer
Faced similar issue and here is how I fixed it. Remember that there are multiple ways to run your code in VS code. And for each way you may end up with different interpreters and environments. For example:
1. Creating virtual env and installing libraries
In my case I opted into creating virtual environment and doing so outside of VS Code using command prompt:
python -m venv .plotting_test
Following that I activated it:
.plotting_test\Scripts\activate.bat
Following that I installed additional libraries:
python -m pip install matplotlib
Following that I made sure to see it was all installed ok:
python -m pip list
And I also checked where for current directory:
cd
2. Point VS Code & VS Code Code Runner to virtual environment
Opened vs code, closed previous workspaces, opened new folder, created test.py as I was starting new. Pressed ctrl + shift + p. Selected ```Python: Select Interpreter``:
Followed by + Enter interpreted path
Navigated to directory from last step from section 1. Found my virtual environment folder created in step one and pointed VS code to that version's python.exe in Scripts:
Verified I am pointed to such:
Saved as workspace so that I can create default workspace settings for this project:
In workspace settings files defined paths to my virtual environment created n step 1 for workspace interpreter & CODE RUNNER(!):
"settings": {
"python.defaultInterpreterPath": "C:/Users/yyguy/.plotting_test/Scripts/python.exe",
"code-runner.executorMap": {"python": "call C:/Users/yyguy/.plotting_test/Scripts/activate.bat && python -u"}
}
}
Reloaded window just to make sure (ctrl + shift + p) = "Developer: Reload Window"
Now run code and run python file should be execute under your specified envs:
Try running pip list in VS Code to check if the module is installed, next check if your python version is correct/supports that version of SimpleITK. It may be a problem with the python interpreter that you are using for VS Code (ie. the module may be installed on a different python instance than the one your VS Code is using)
Is Python environment selected?
Does the Terminal recognize the Python environment?
Can I run the Python code from the activated Terminal?
Does the code run w/o error when I use "Start Debugging"?
if the answer to the above is "yes."
Then,
Try running the Code using the option "Run python file in terminal" (in code runner extension). And assign a new shortcut for that for future use...
How to fix module not found error in Visual Studio code?
To Solve VSCode ModuleNotFoundError: No module named X Error Make sure you are running from the package folder (not from package/module ) if you want import module. calculations to work. You can also set the PYTHONPATH environment variable to the path to the package folder.
Once you have created a virtual environment, and installed your required packages in that environment, close VS code. For Windows platform, open command prompt and navigate to the folder where your virtual env folder is created. And then launch VS code from there using the command code .
For ex: My virtual env name is .imgenv, and its inside C:\py_stuff\projects
So, I navigate to C:\py_stuff\projects and then type code .
Now, your VS code should recognize the packages !
I just ran into the same issue. I found that if I selected all text before shift enter the script would compile as a file instead of as a single line.
I had the same problem. I bet you have a shebang statement at the top of your file.
If you do.
Visual Studios settings
Under "Code-runner->Code-runner: Respect Shebang" section or just do a search for "Code-runner: Respect Shebang"
Uncheck weather to respect Shebang to run code.
Now it will run under the virtual environment and find the modules that you installed using pip! :)
I struggled with this for a very long time, and had tried almost every other answer. I wasn't using pip, so that wasn't the issue. But still VS Code wasn't finding the modules that were installed in the Selected Interpreter.
Ultimately it came down to old conflicts that existed because I switched to miniconda, and VS Code was still looking for anaconda3.
I completely wiped VS Code and its associated files (cache, preference files, etc.) from my machine (some instructions), and installed a clean version.
This now syncs as expected with miniconda.
If you have different python versions installed, be sure you install module with right one.
python -m pip install <module>
or
python3 -m pip install <module>
Run your environment from a directory not in the users directory. I solved my problem running my environment from C:\Code\ProjectA\
I discovered my problem by running:
IMPORT os
Mycwd = os.getcwd()
PRINT(Mycwd)
.venv/Lib/SitePackages is the default directory where Vscode looks for Modules.
This directory is automatically created on creating .venv via the command Pallete.
External modules installed via pip are placed in this directory by default.
Place self created modules inside this folder manually.
For mac users
In the terminal check which python you are using by command which python. It will give you the path of the python interpreter path. After that type cmd shift P and type Python: Select interpreter.
After that select + Enter interpreter path and paste the path which you got after running the command which python.
I use PyCharm Edu 2018.3, with a virtual environment path set outside my python application root folder (I use this environment for other python code).
When i run the program from PyCharm, it works without errors. But when i run it from a .bat file it fail to be able to import the packages from my environment.
Basically, it seems like all installed packages made in the environment, are not being found when launched from the .bat file.
So i was wondering if the fact that my environment was outside my root python code was an issue or if i had to precise a path, or else.
All packages imported are correctly inatlled in my envirnment and the code runs as expected when run in PyCharm.
Also, i tried to run a simple "Hello World' code from my .bat file and it works correctly, so issue only occurs when calling packages from my environment.
Could it be a setup file issue (i have none)?
Thank for the help and suggestions.
For instance, the import of selenium which is my first import, in the code, triggers the following error:
#
from selenium.webdriver.firefox.webelement import FirefoxWebElement as MyWeb
Element
ModuleNotFoundError: No module named 'selenium'
#
I'm guessing you have either installed selenium to the virtualenv that PyCharm setup for you when executing from withing the IDE. However When your running outside of the IDE you are using the default python interpreter not your virtualenv. To ensure your in the expected virtualenv make sure it's activated before running.
So for example
$ cd C:\Users\'Username'\venv\Scripts\
$ activate.bat
From this point you should be able to execute your bat script using that virtualenv.
If your still getting it with the virtualenv activated then try installing with pip while your virtualenv is activated.
im trying to test Machine Learning codes from this site :https://github.com/lyuboraykov/flight-genie
im really new to machine learning and im using windows.
i already installed the requirements to run the code (python, virtualenv, numpy, sklearn, scipy, etc) but i got stuck when i try to run the main code, it shows notification like this :
please help, thanks
If you run a python script as
python dir_name/script.py
... then python will execute the file named script.py inside the folder dir_name. What will happen here is python program will go inside dir_name folder first and then run the script.
In your case if you type python flight_genie/main.py, it will go inside the folder flight_genie and will execute the file. Then python can't find a folder (actually the module) named flight_genie there because python program is already inside that folder. That's why you get this error.
So one way of fixing this issue is replacing all import flight_genie.xxxx with just import xxxx. (Also from flight_genie.xxxx import yyyy with from xxxx import yyyy)
But it is so time consuming if you have a large project. (And sometime it won't even work). So best way is to run the project as a whole module.
If you look at here you can see how to run python modules as scripts. You just have to type the following command in console.
python -m flight_genie.main
ps: I assume that you have python3 installed in windows and configured to run python3 when you type python in command line.
Have you made installation steps described on author's page? I'm afraid that you didn't read this...
sh
# you have to have python 3 installed
pyenv env
source env/bin/activate
pip install -r requirements.txt
python flight_genie/main.py
Based on the error message I think your problem is with your import. I'm guessing it should be something like:
from flight import Flight
Instead of:
from flight_genie.flight import Flight
If that doesn't work try to post your code so we can try to find the problem.
We have a python 2.7 script that executes LOCALLY perfectly fine.
The problem occurs ONLY when I attempt to invoke the script remotely (SSH):
ssh user#server "python script.py"
*Traceback (most recent call last):
File "script.py", line 18, in <module>
import requests
ImportError: No module named requests*
After doing a little bit of research, I have tried manually inserting the absolute the path to the "requests" library using sys.path.insert before importing the library:
sys.path.insert(1,'/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg')
import requests
I even confirmed the path to the package got added succesfully by printing the sys.path when invoking the script remotely:
print '\n'.join(sys.path)
Result:
/usr/local/lib/python2.7/site-packages/setuptools-20.9.0-py2.7.egg
/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
However, I am still getting the "ImportError: No module named requests" no matter what.
Can you help me understand what am I missing?
Again, this script does find the library without issue and runs perfectly fine when I execute the script locally.
Thanks!
requests is a non-standard Python library. Make sure it is installed on the remote box you are attempting to execute the script on.
On the remote, execute from the shell:
easy_install pip
pip install requests
If I understand correctly, you are trying to run the same script on the same machine, but once from a local shell and once from a remote shell (with ssh).
If this is the case, when you use the remote shell, be sure that you have the environment properly setup.
This may boil down to just setting the PYTHONPATH environment variable correctly.
In your case, this would be:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/:$PYTHONPATH
You may need to add more paths to it.
When I have this sort of issues, I usually run a python shell from the working environment and check where the files are taken from, so I can be sure the correct entries are set in the PYTHONPATH:
$ ipython
In [1]: import requests
In [2]: print requests.__file__
/usr/local/lib/python2.7/site-packages/requests/__init__.pyc
For this example, I would add /usr/local/lib/python2.7/site-packages/ to my PYTHONPATH if I was not able to import requests correctly.