Custom python module seen in C: but not in D: - python

I have a module that I want to be able to import from any script without
doing any sys.add_path and similar stuff. I want it to be permanently added.
Since I installed Python with anaconda, I have a anaconda3 folder added to Path under
System variables. In the same place I added C:\Users\lukas\anaconda3\package and in the
package folder is my script with empty init.py, and the script.py which contains functions.
In the Path under User variables, I already had anaconda3 folder, and I added the \anaconda3\package.
Also, I created the PYTHONPATH variable under User variables and added anaconda3 and \anaconda3\package.
When I open my CMD, that is working in C drive, it can import the package successfully.
But, when I open the CMD in D drive(in my VS code) it is not working, can't import the package.
As you can see, I've tried everything I saw on the internet, and probably now I have much more junk than needed added paths.
How should I clean this up and make it work on every drive?

For anyone that is interested,
after several hours, I tried again(by accident) and
everything works :)
It seems like Win10 needed some time to really update the environment
variables.

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!

Why doesnt python see module?

I have the next problem with Python 2.7.3:
There's little project with the next structure (see at the end); through virtual environment Python doesn't see a module, but before it did.
I've already tried rebooting and reinstalling virtualenv directory—nothing. Also, if I run the necessary script file out of any virtual environment (using ipython), it finds that module. I've been looking for the problem all night. Can anybody help me? For example, I'm trying to run the script from delete_ME_after.py (inside of it there's importing module base.config_parser).
Also, I've already set path to this project to PYTHONPATH—also nothing works.
As it turns out, the above PYTHONPATH variable contains a tilde ~ which isn't expanded to the full home directory as expected. See here for more information.
Use one of the following instead:
export PYTHONPATH=/path/to/home/smm
export PYTHONPATH=${HOME}/smm

How to run project files using Anaconda from any directory in Windows

Python newbie here. Am using Anaconda (on Windows 7) by recommendation of a friend.
What I want to be able to do is make a change to my class in Notepad++ and then test it in my Python command prompt window immediately.
This is a simple question that I can't seem to find the answer: in what directory does a default installation of Anaconda expect me to be storing my .py files (so that I can easily load them using import <MODULE NAME>)?
My PATH variable is set to:
C:\USERS\<USERNAME>\Anaconda3;C:\USERS\<USERNAME>\Anaconda3\Scripts
(this is the default)
Am I supposed to be working out of the Scripts directory...? There's already a lot of files in there.
What do most people do? Perhaps add another folder to the PATH variable and work out of there? Do you add a new folder to PATH for each project or is there a way that makes more sense? I already have a Projects directory I use for everything else. I'd like to just be able to work out of there.
I am coding in Notepad++. I don't really want to bother setting up/learning an IDE (I'm just doing relatively simple I/O file manipulation that I have previously been doing in Excel... the horror).
Sorry for the extremely newbie question. I searched and could not find anything relevant.
EDIT AFTER ACCEPTED ANSWER:
The problem was that I was running python.exe from the Start menu. I did not realize that you are supposed to open a CMD window in the folder (SHIFT+RIGHT CLICK) you are working in (such as C:\USERS\<USERNAME>\MY PYTHON STUFF) and run python from there.
This might be what you're attempting. Note that I also use Anaconda.
My path:
C:\Users\...\Documents\Python Scripts\
import_sample.py
class class_sample(object):
def __init__(self):
self.x = ["I", "am", "doing", "something", "with", "Python"]
test.py
from import_sample import class_sample
c = class_sample()
y = c.x
print " ".join(y)
Result:
I am doing something with Python
[Finished in 0.1s]
Notice how being in the same folder allows me to import without having to install, per se. Basically, just make sure that the modules you need are in the same folder as your main.py and you're good.
EDIT:
Done from the terminal.
Notice how I cd into the above folder and activated python there. Since I was inside that folder, any modules inside it can be imported without any problems, alongside other system-wide modules installed as well.

What is the purpose of the environmental variable PYTHONPATH

On windows 7, I currently don't have a python path. Can I safely make one? If so, how do I do it?
Upon making this variable, I can no longer load Spyder (IDE) without it crashing. Does anyone know why?
I would like to edit my existing python path if possible, but just don't know why it isn't already there in environmental variables.
I would ultimately like to be able to run "python myscript.py" and have myscript be in a different directory from the call directory.
PYTHONPATH adds new paths to the ones Python uses by default. The path in total determines where Python will look for modules when you import them.
Look at sys.path to see the combination of the defaults with your PYTHONPATH environment variable. It's likely that Spyder is loading a module that exists in two different places and the wrong one comes first.
When you import modules in python, python searches for the module in the directories in PYTHONPATH, in addition to some other directories.
In order to be able to run your script as > myscript.py, you want to put your script somewhere on PATH (here are some instructions for viewing or updating PATH), this is where the OS looks for scripts and programs when you give it a command. I believe that in windows the .py extension must be associated with python for windows to know that myscript.py should be run using python. This should happen automatically when python in installed, but maybe someone with more windows knowledge can comment on this.
it has role similar to path. this variable tells the python interpreter where to
locate the module files imported into a program. it should include the python source library directory and the directories contain in python source code

Setting python path

I have a Django app and I'm getting an error whenever I try to run my code:
Error: No module named django_openid
Let me step back a bit and tell you how I came about this:
I formatted my computer and completely re-installed everything -- including virtualenv, and all dependent packages (in addition to Django) required for my project based on settings in my requirements.txt folder
I tried doing python manage.py syncdb and got the error
I googled the issue, and many people say it could be a path problem.
I'm confused as to how I go about changing the path variables though, and what exactly they mean. I found some documentation, but being somewhat of a hack-ish noob, it kind of goes over my head.
So my questions are:
What exactly is their purpose -- and are they on a system based level based on the version of Python or are they project dependent?
How can I see what mine are set to currently?
How can I change them (ie. where is this .profile file they talk of and can I just use a text editor)
Any input you would have would be great as this one is stumping me and I just want to get back to writing code :-)
The path is just the locations in your filesystem in which python will search for the modules you are trying to import. For example, when you run import somemodule, Python will perform a search for somemodule in all the locations contained in the path (sys.path variable).
You should check the path attribute in sys module:
import sys
print sys.path
It is just a regular list, sou you could append/remove elements from it:
sys.path.append('/path/to/some/module/folder/')
If you want to change your path for every python session you start, you should create a file to be loaded at startup, doing so:
Create a PYTHONSTARTUP environment variable and setting it to your startup file. E.g.: PYTHONSTARTUP=/home/user/.pythonrc (in a unix shell);
Edit the startup file so it contains the commands you want to be auto-executed when python is loaded;
An example of a .pythonrc could be:
import sys
sys.path.append('/path/to/some/folder/')
Do you really need to alter the path? It's always best to actually think about your reasons first. If you're only going to be running a single application on the server or you just don't care about polluting the system packages directory with potentially unnecessary packages, then put everything in the main system site-packages or dist-packages directory. Otherwise, use virtualenv.
The system-level package directory is always on the path. Virtualenv will add its site-packages directory to the path when activated, and Django will add the project directory to the path when activated. There shouldn't be a need to add anything else to the path, and really it's something you should never really have to worry about in practice.

Categories

Resources