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~
Related
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"
]
im very new in programming and i learn Python.
I'm coding on mac btw.
I'd like to know how can i import some modules in VS code.
For exemple, if i want to use the speedtest module i have to download it (what i did) and then import it to my code. But it never worked and i always have the error no module etc.
I used pip to install each package, i have them on my computer but i really don't know to import them on VS code. Even with the terminal of the IDE.
I know it must be something very common for u guys but i will help me a lot.
Thx
Quick Summary
This might not be an issue with VS Code.
Problem: The folder to which pip3 installs your packages is not on your $PATH.
Fix: Go to /Applications/Python 3.8 in Finder, and run the Update Shell Profile.command script. Also, if you are using pip install <package>, instead of pip3 install <package> that might be your problem.
Details
Your Mac looks for installed packages in several different folders on your Mac. The list of folders it searches is stored in an environment variable called $PATH. Paths like /Library/Frameworks/Python.framework/Versions/3.8/bin should be in the $PATH environment variable, since that's where pip3 installs all packages.
Most probably you have multiple versions of python on your computer.
You have to select your python interpreter for which you have installed those packages using pip to do this you'll have to click on the python version you see in the bottom and select the correct interpreter from the list:
You can also find more information within the VSCode docs.
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.
New to python and I cannot import modules that I have installed via pip.
For instance, I have installed numpy though cannot import it.
I have a feeling from trying to work this out that it is installing to the wrong directory, or I am calling the wrong version.
$ which python
returns
/usr/bin/python
I am just not sure how to change it so I can access the modules.
First if all, you are installing the packages using pip, which means you install it on python 2 by the default configurations.
The issue you are describing can be caused by several problems:
If you are working with an IDE like pycharm- your project interpeter might by python 3.x. You should change it to python 2 since you used pip and not pip3.
Some newer versions of pycharm are opening virtual environment by default in new projects. This means that if you install packages outside the virtual environment you will not be able to access them. When opening a project, intended of applying the default settings, change the interpreter to your system interpreter, probably your python2.7 in your case.
You are not using an IDE but accecing python from your terminal like so: python3 instead of python.
Hope it helps ;)
From a terminal try:
pip install numpy --user
This install numpy to your home directory. Sometimes this helps compared with installing it without the '--user' flag.
Then:
python
Now you have a python command line. Try:
import numpy
If you don't see any error message, then the install worked. Control-d or typing 'exit()' returns you to your shell.
this is my first time here, and I'm fairly new to python, so please let me know if you need more information. Thanks in advance.
I am running python 3 on Windows 7
I discovered my problem after I used pip install numpy. This works just fine. Then, when I try to use import numpy in the python shell I receive the ModuleNotFoundError: No module named 'numpy'. That's when I noticed that my default version of python was 3.6.1, despite having updated to 3.6.2 at some point. I still have both .exe setup files and when I run them it shows that I only have 3.6.2 installed. However, when I type python --version in the command line i get Python 3.6.1, even though Python36-32 is what i have in my path.
I think my question is how can I make sure I'm running the newer version of python as my default, or if need be, how can I get rid of the older version?
When you type python in cmd, it searches python command inside the directories in the environment variable named Path. Actually Path includes both python directory and python scripts directory. For example, in my computer, Path includes:
C:\Users\user\AppData\Local\Programs\Python\Python36
C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts
pip is in the python scripts directory. Your Path can be wrong. You should check it. This link can help you. You should have one python directory and one python script directory in Path, just the version you need.
Also, you can call pip as a module:
python -m pip install numpy
This will install the package to the version which is in the Path, Python 3.6.2 in your situation.
If none of these works, I recommend you to uninstall(delete) Python 3.6.1, and try to use pip again. If pip doesn't work(or disappeares), you can read this or use get-pip.py to install pip to your computer again. Maybe, you can delete all python versions, and install the version you need, and of course, you should be careful about Path again.
EDIT:
I am not sure about your problem. Some informations are needed for a certain solution.
You can find the source of an executable(python or py in your situtation) with where command. Here is an example from my local:
where python
Output:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe
That means C:\Users\user\AppData\Local\Programs\Python\Python36-32\ directory is in my Path and when type python, cmd runs python.exe.
So, you can find out what are py and python exactly.