Manually Delete Python Package Path - python

I manually removed /Library/Python/2.7/site-packages/google through terminal (rm), however it still seems I can import the package in python 2.7.
I am able to run import google but when I print google.__path__ it displays ['/Library/Python/2.7/site-packages/google'] even though that directory no longer exists because I deleted it.
I initially deleted this package because it was giving me import errors when trying to run google's app engine api, so I need to have import google be unlinked to this directory.
Any help would be greatly appreciated!

Try starting python in verbose mode. This will show from where packages are imported. Since the output can overflow, write it to a text file.
python -v 2>&1 | tee out.txt
>>import google
>>exit()
Open out.txt and see from where google package is being imported.
As suggested earlier, import issues can be avoided by using virtualenv.

Related

My Python Script can't find a JSON file in the same directory

I am practicing some API work in python 3.7 using API star 0.5.X and my python script can't find the .json file that is in the same folder as the python file. I am working on and running the script with Atom editor and I am working in a venv, which is fairly new to me.
I am using a helper function to load in the JSON data using a "with open()" statement. I have tried using the relative and absolute file paths, and in both instances it is unable to locate the file. I have tried launching the file in Atom using terminal and the MacOS finder.
This is what I have so far:
import json
from typing import List
import os
from apistar import App, Route, types, validators
from apistar.http import JSONResponse
print(os.getcwd())
os.chdir('/Users/{myusernamehere}/100days/apistar')
print(os.getcwd())
#helpers
def _load_employee_data():
with open('employees.json') as f:
employees = json.loads(f.read())
return employees
the second print statement prints the correct file path, being the one that 'employees.json' and 'app.py' are located in.
Since the problem is specific to your setup, it's hard to reproduce or provide a solution in code. Your code itself looks to be fine, but there are two things that are likely to be the cause of your issues:
When your script is running, Python needs access to the appropriate source folders and installed packages; you should let something like virtualenv manage this through a virtual environment. From the terminal, you can load the appropriate virtual environment with:/path/to/your/venv/Scripts/activate.sh
If you do, you should expect your script to find the same libraries it did during development in that virtual environment. Make sure you include something like a requirements.txt in your project to allow easy reinstalling of the same modules on a different machine, in a new virtual environment.
Your script, when run by Python, has a 'working directory'. This is the directory that Python is started from and your script not being able to find the file (even though it may be in the same folder as the script itself) is probably due to Python being started from a different directory.
This was a problem due to how the Atom editor works. It was solved by switching to vim.
I only partially understand but apparently this had something to do with Atom having a separate temp directory for working files, or something of that nature. When using vim to edit the script, and then calling it in the terminal the problem was resolved.
Okay, So i had the same issue with i.c.w. VScode :
file = open('file.txt')
print(file.name)
resulted in
FileNotFoundError
file.txt was 100% in the same folder... According to finder on my Mac, ánd the folder column in VS code!
i was pulling out my hair. Switched a lot of interpreters, local python and Conda, to Python 3.8 instead of 3.9, back to python 2.8.
Nothing mattered.
Till I changed :
file = open('file.txt') to: file = open('file.txt', 'a')
It didn't suddenly work, but I saw immediately in the "folder column" of VScode a new file.txt file popping up. In an entirely different folder then where the pythonfile.py was located. So after that; I pushed all local repo's to their remotes; deleted the whole caboodle, and installed them one by one in a new folder through git clone. I opened a new workspace added those project folders and since then it works like a charm.
What exactly went wrong ; im sorry, I have no idea. But for me, a fresh install of the repo's and VScode workspace is what did the trick.
I recently had the same error, on Visual Studio Code, I managed to solve it by instead of clicking the Run Python button, I used the terminal to cd into the project directory and run the python script like that, and no problems!

Azure DevOps Pipelines - Python - ModuleNotFoundError despite sys.path.append() or setting PYTHONPATH

I'm trying to run some tests for my python application but I'm not able to set the path correctly so that my test.py can find it.
My Application is structured like this:
repo/src/main/python/main_module
repo/tests/test.py
And my test.py is looking like this:
import sys
sys.path.append(os.path.normpath('C:/repo/src/main/python'))
import main_module
Now I want to test the code within Azure Pipelines, so first I copy the repo in place:
- powershell: |
cd C:/
mkdir repo
cp -r -v $(src.repo)/* C:/repo/
condition: eq( variables['Agent.OS'], 'Windows_NT' )
After that I use tree to test if everything was copied correctly.
And then I simply run the test script by calling:
python C:/repo/tests/test.py
But that gives me a ModuleNotFoundError.
I've also tried setting the path to my main_module via PYTHONPATH but that's not working too.
Is their something I missed or is this a bug within Azure Pipelines?
After talking to the Azure DevOps Support, I now know that my issue is a bug within DevOps-Pipelines at the moment.
For now I'm going to copy my test-scripts next to my main module and delete them if everything was successful and before building the application.
To execute python script with Azure pipeline in Linux enviroment, I used 'script:'
script: pythonpath=/repo/src/main/python python3 repo/tests/test.py
displayName: 'execute script python'
Based on the symptoms I believe this is the same issue as:
Azure pipeline can't find the user created python module
I have solved it with the following workaround
include a script in the pipeline to move the source code into a new
suitably named directory and then test it there.
See my answer to the other question for full details and example YAML

How to Run Python script that contains xlwings package in Command Prompt

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

Python Package Module Not Found SVN Import

Imported the SVN Package, going to the following PYTHONPATH confirmed by import sys >>> sys.path:
\ProgramData\Anaconda2\lib\site-packages
This is where all packages are stored and other packages within this directory will import appropriately in Py.
When I open Py in Terminal, and Import SVN, it is producing an import error, No Module Name. Tested this in Terminal, IPython, Bash, Jupyter Notebook.
The only thing I can ascertain is this is an issue perhaps with a third party library...out of ideas at this point. There does not appear to be another option for a SVN module, need this for script to grep SVN. If there's another way to do this without this package, welcome other ideas as well.
Thank you in advance for any help or ideas.

Google App Engine cannot find gdata module

I can run a simple "Hello World" Google App Engine application on localhost with no problems. However, when I add the line "import gdata.auth" to my Python script I get "ImportError: No module named gdata.auth".
I have installed the gdata module and added the following line to my .bashrc:
export PYTHONPATH=$PYTHONPATH:/Library/Python/2.5/site-packages/
Is there anything else I need to do? Thanks.
EDIT: The strange thing is that if I run python from a shell and type "import gdata.auth" I do not get an error.
Your .bashrc is not known to Google App Engine. Make sure the gdata directory (with all its proper contents) is under your application's main directory!
See this article, particularly (and I quote):
To use this library with your Google
App Engine application, simply place
the library source files in your
application's directory, and import
them as you usually would. The source
directories you need to upload with
your application code are src/gdata
and src/atom. Then, be sure to call
the
gdata.alt.appengine.run_on_appengine
function on each instance of a
gdata.service.GDataService object.
There's nothing more to it than that!
The gdata client library installation script installs the modules in the wrong directory for ubuntu python installation.
sudo mv /usr/local/lib/python2.6/dist-packages/* /usr/lib/python2.6/dist-packages
try adding this to your script:
import sys
sys.path.append('<directory where gdata.auth module is saved>')
import gdata.auth

Categories

Resources