I'm a python beginner, and I'm following this tutorial for a webscraper https://hackernoon.com/building-a-web-scraper-from-start-to-finish-bb6b95388184
I'm on Windows 10, have setup a venv, activated and installed 2 modules using pip, and moved my script into the Scripts folder (from my understanding, this is the equivalent of the /bin/ folder on linux installations). The modules are bs4 and requests. I see both of these in the /Lib/ folder of my venv. I am using the Atom editor from atom.io, and the Scripts Package to run my script.
My script errors with a "module not found" error. Relevant snippet below:
scraper.py
from bs4 import BeautifulSoup
import requests
I get the error on both imports, indicating I've setup my project/imports incorrectly. I have no shebang line in my script, and suspect this is the problem.
My project structure looks like:
\ScraperProject
|-\ScrEnv
|-\Include
|-\Lib
|-\site-packages
|-\bs4
|-\requests
|-\Scripts
|-scraper.py
|-pyvenv.cfg
What is the proper way for me to import these modules into my script in a Windows environment?
with what python version are you working in the venv? Maybe try to uninstall those update pip and reinstall those again, making sure that you install them with pyhton3
My issue is related to running it from the Atom editor, something I hadn't considered before. It's calling the python executable from my PATH variable, and not the one in my venv. When running the script from the cmd window, and calling it with the python from my project folder/venv, it runs as expected. On to figuring out how to configure Atom to use the venv executables. Thank you very much for the interest and help!
Related
I want to run a python script with the cherrypy module. I use pip install cherrypy to install it and all looks ok. Then I click the "Run python file" button and I face the error ModuleNotFoundError: No module named 'cherrypy'.
Trying to reinstall cherrypy I see many Requirement already satisfied responses.
If it helps I have already edit, in the user path variables panel, the Path variable and added C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts as a path.
edit: I suspect some problem with the path. So i run the following.
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
it returns: 'C:\\Users\\username\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0'
Have you tried to install cherrypy using C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts\pip install cherrypy
When I install py libraries I always run where python and find the right python copy it and modify the path so it goes in Scripts then run pip install cherrypy
Also I noticed that when I install py libraries it seems VSCode won't recognize it even though it runs. To fix this you have to restart VSCode again
Another possibility is that your VSCode isn't using correct version of Python. To check this look at VSCode bottom left you should see your python version. If VSCode can't recognize it you will have to enter the Python path manually by Open Command Palette Ctrl+Shift+P and choose Python: Select Interpreter then click enter Interpreter path
Running PyCharm 2020.1.2 Community Edition in Win10 with a Python 3.6 venv as interpreter. Installed the package feature-engine through the Project Interpret interface, installs fine and appears in the list. I can successfully import feature_engine in the PyCharm console, and I can use it fine. I can also execute a .py file with this import statement in the Terminal with the venv activated, and it also works fine. However, when I try to Run the same .py file with the import statement, I get:
ModuleNotFoundError: No module named 'feature_engine'
I have tried using import and importlib, thinking the issue was the hyphen, but those didn't work. I have tried uninstalling and reinstalling, restarting PyCharm, etc. Nothing seems to work. Any suggestions how to get the Run function working?
EDIT: Thanks for the suggestions. Attached are the Run configuration and the Project interpreter configuration. As far as I can tell, the environment is the same.
Below are examples of the error trace. The object being Run is a Flask app, which imports packages that use the feature-engine library. The actual import statement in the final import is simply import feature_engine. Trying to import the method directly using from feature_engine import variable_transformers as vt also fails.
Make sure you're using the right configuration to build your program (that is, using the python executable of the right enviroment). You can check this in the top right corner, where the run button is.
I have build my own package named "XXX" on my computer by setting a __init__.py file in the C:\path\to\module\XXX folder, which contain my python script. Then I added C:\path\to\module\XXX to my pythonpath manually. When I run python on a cmd prompt and try to import the module it fails. When I print sys.path, it contains C:\path\to\module\XXX. If I try to load some other personnal modules, the same problem occures but when I try to load module not of my own, installed with pip (i.e. numpy) it works perfectly. So my guess is that for some reason python is not looking into my pythonpath anymore to load module.
I am using anaconda with python 3.6.5 with a vitual environnement on windows 7. Python is installed with anaconda via conda-forge repo.
I have tested this issue with multiple approach, first by running the script below with Pycharm and then with Spider. Then I used anaconda prompt to run the commands manualy activating my virtual environnement first. Finally I ran the commands on a DOS prompt with python. Whatever the way, the results where the same. This error just occured after installing fbprohete in my virtual environnement. Before that I was able to import my modules. I tried to remove that module but the problem remains the same. A lot of mess append during the installation of fbprohet. After the installation I ran a conda clean command to clean the environnement but it didn't help. I suspect that something is corrupted now but I would like to avoid reinstalling everything. If you have an idea where to look, which module in particular should be corrupted, I will be glad to know more about it.
import sys
sys.path
['', 'C:\path\to\module\XXX', ...]
import XXX
ModuleNotFoundError: No module named 'XXX'
Whith the script above, I expect the module to be loaded without error since the path to the package is in the pythonpath and the __init__.py is in the folder C:\path\to\module\XXX
Thanks!
So it was a bit silly actually. I should have replaced C:\path\to\module\XXX by C:\path\to\module in the pythonpath because __ini__.py is in the C:\path\to\module\XXX folder. Thanks Giacomo Alzetta for your comment.
I'm trying to run python3 script in command prompt Windows(cmd) but facing some issue if python script contains xlwings package. I'm using xlwings package to read and write the information that I needed in excel file. I had go through some research but all of the solution is pointed to run python from excel (meaning call python script in vba) and I don't want to do that. Here is the sample of the python code for testing purpose.
import xlwings as xw
bookName= r'C:\Users\Desktop\Python_Projects\Test.xlsm'
wb= xw.Book(bookName)#specified full name of excel file
sht = wb.sheets('Sheet1')
app= xw.apps.active #need to kill excel apps
sht.api.Cells(1,1).Value="test"
sht.api.Cells(1,1).Font.Bold = True
wb.save(bookName)
wb.close()
app.kill() #or del app
I'm trying to run the script and hit this issue:
I'm running the code from my IDE Pycharm, no issue and able to run. Note: I don't have admin right permission in my Windows.
Addition:
I had try to search and find the post similar with my environment(using Pycharm-not run via terminal, setup as a virtual environment) but different issue facing. I'm not very understand the answer in this post. Thus, not sure whether the answer is fixing my current issue or not. If the solution is same, hope that someone can describe it further details. Here is the link:
Python script works in PyCharm but throws path errors in windows cmd
Moving comments down to answer...
Sounds like PyCharm had been setup to use a virtualenv, so you found a link where you found that you need to activate the virtualenv
From there, that's where any PyCharm ran a pip install into, and from which you can run python and try to import any modules.
If you don't activate the environment, you would need to run your scripts by giving the full path to the Python executable - C:\path\to\venv\python script.py
To backup a list of installed modules from a virtualenv, you can do pip freeze > requirements.txt
Then pip install -r requirements.txt will restore those into any freshly created virtualenv
I'm working on a project for which I need to a module. I want to know how to import some other module in python that is not installed.
When I write
import xaut
It gives error no module named xaut.
I have xaut-0.4.5 that I downloaded from following link.
Please help me how to use it.
Well I think following will help
Extract your zip file
Open the command line (a linux terminal or cmd on windows). I am on linux so I have a terminal.
Now enter the directory that you have extracted.
In it is a directory "python" cd into it.
If you run the ls command (if on windows run dir) you will see that in this directory there is a script "setup.py". We need to execute it for installation.
Execute this script by giving command python setup.py install
This will hopefully install it and then you would be able to import it.
Basically you have only downloaded the package. To make a package work you also need to install it. So when you download a package always search for a setup.py file.
This is certainly a duplicate question. You can look up how to add a python module to the path in windows.
here is an example
How to add to the pythonpath in windows 7?