this is in Python, python 3.10.2 if we need specifics.
I’m using the Mac Terminal and I want to access a folder from it
I have a folder on my desktop that has a bunch of my modules. I wanna access these from the terminal in a style like: “import module” with using some command to get to that folder.
Help would be appreciated!
You can import sys, then sys.path.append(path_to_desired_modules). sys.path is a list of the directories where Python will search for the modules to import, so if you append the target directory to that, it should be retrievable.
Related
I realize that a lot of people have had this question but I've tried their solutions and they haven't helped me. I watched this video here but his solutions is not helping me:
fix problem
I can get python to work on the python app and using it the path of the python interpreter is
import sys
print (sys.executable)
>>> C:\Users\Owners\Documents\Python
I then set the environment variable like so:
I've also tried attached 'python.exe' but that didn't work either. When I then write python in the terminal I get the error message in the terminal.
You should make a new environment variable in your system variables named PATH if it doesn't already exist then you need to add your python interpreter path to that variable.
Adding Python.exe file in Path won't work as it is just an executable file, also which version of Python have you installed because according to this python 3.9.0 and above won't run on Windows 7 or earlier.If this is not applicable in your case then click on New in your system variables menu and add PATH as name and locate the Python Application Path and Python Scripts Path in folders (i.e where you installed Python) and add it to the newly created PATH
I've written my own mail.py module in spider (anaconda). I want to import this py file in other python (spider) files just by 'import mail'
I searched on the internet and couldn't find a clearly solution.
To import any python script, it should exist in the PYTHONPATH. You can check this with the following code:
import sys
print sys.path
To import your Python script:
Put both the scripts (main and the imported python script) in the
same directory.
Add the location of the file to be imported to the
sys.path.
For example, if the script is located as '/location/to/file/script.py':
import sys
sys.path.append('/location/to/file/')
import script
I had the same problem, my files were in same folder, yet it was throwing an error while importing the "to_be_imported_file.py".
I had to run the "to_be_imported_file.py" seperately before importing it to another file.
I hope it works for you too.
Searched for an answer for this question to. To use a .py file as a import module from your main folder, you need to place both files in one folder or append a path to the location. If you storage both files in one folder, then check the working directory in the upper right corner of the spyder interface. Because of the wrong working directory you will see a ModuleNotFoundError.
There are many options, e.g.
Place the mail.py file alongside the other python files (this works because the current working dir is on the PYTHONPATH.
Save a copy of mail.py in the anaconda python environment's "/lib/site-packages" folder so it will be available for any python script using that environment.
I did a slightly different solution approach that is less sophisticated. When I start my anaconda terminal it is at a C prompt. I just did a cd d:\mypython\lib in the beginning window before starting python. once I did that I could simply just import my own classes that I put in that library with "import MyClass as my" then I was off and running. It is interesting, I did 2 days of internet searching in my part time and could not find the answer either, until I asked a friend.
cd d:\mypython\lib
python
>>> import MyClass as my
>>> my1=my.MyClass()
>>> my1.doSomething()
worked for me on my anaconda / windows 10 environment python 3.6.6.
when calling any function from another file, it should be noted to not import any library inside the function
I believe the easiest solution is to place the directory containing your Python files into the Anaconda site-packages folder on your machine. I wrote an article outlining the whole process but, in short, you'll need to create a folder containing your python script and an __init__.py file. Then place that folder inside the site-packages folder in the Anaconda directory.
On Windows the site-packages directory is typically located at:
C:\Users\[your_username]\Anaconda3\Lib\site-packages\
On Mac the site-packages directory is typically located located at:
Users/[your_username]/opt/anaconda3/lib/[python3.8]/site-packages/
Notice, on Mac the Python version matters. You'll need to look for the directory that corresponds to the (base) Python version used by Anaconda. Also, anything I've placed inside of square brackets in the file paths above need to be changed according to your particular machine and Python version.
The file structure should look something like this:
~/
|__site-packages/
|__your_folder/
script.py
__init__.py
After you have the folder containing your script.py and __init__.py file moved into the site-packages subdirectory of Anaconda, you'll be able to import it from any script you run on your machine.
I am a Python beginner and I am having trouble running Python from CMD. I have added the Python installation directory as a PATH variable (;C:\Python27). I am able to run the Python Interpreter from CMD, however when I issue a command like "python file.py command" from CMD, it returns "Error2, Python can't open, no such file/directory".
So what I do is go to "cd C:\Folder\Folder2\My_Python_Files", then type the "file.py command" each and every time. Is there faster or more efficient way of doing this? I am currently running Python2.7 on Windows 8.
When you run python <script>, it requires an actual path to the script being provided. You cannot specify "file.py" alone, unless it is right there in your current directory.
In windows, here are two steps you can take:
Associate .py files with python. Then you can run them directly without the python command as: /path/to/file.py
(right click a .py -> properties -> change to associate with python.exe)
Further step: Add a location to your PATH environment which will contain your python scripts. From there, you can just do file.py and it will be found in your search path.
So you could add C:\Folder\Folder2\My_Python_Files to your PATH and that is where you can store your executable python scripts.
Also you can set the PATH variable temporarily during a shell session:
SET PATH=%PATH%;C:\path\to\project
Just like PATH environment variable lists several directories for the system to search for executables, the PYTHONPATH do the same for Python to search for .py files. If you want scripts in a folder to be globally accessible (i.e. you can reference them by name just like you want, or you can import them from other scripts), add that folder to PYTHONPATH (create it if it doesn't exist).
Note that the command to invoke a script that is in your PYTHONPATH is:
python -m file [<script arguments>]
(i.e. use the -m option to treat it as a module, and don't use the extension .py)
Here's an article explaining in more detail how Python finds its source files (both in the command line and through import).
Note that you can also refer to the script by using its full path:
python C:\Folder\Folder2\My_Python_Files\file.py command
But by doing this, other files in the same folder that this script might reference through import might not work (since Python doesn't know where to search for them).
Unless the project's folder is in the PATH, you cannot call the file unless you are inside the project's folder. Don't create PATHs for projects, unless they are needed; it's unnecessary.
Just transverse to the file's directory and run the command inside the directory. That will work.
If the project will be used by other projects/files, you can use PYTHONPATH to set the directory, so the other projects can successfully access it.
Hope that helps.
I'm trying to learn python and I'm having trouble importing a module.
I have a .pyc file that I'm trying to import into idle shell called dfa.pyc
I have the file in a folder called xyz.
I navigate to this folder using:
os.chdir('/Users/xxx/Desktop/xyz')
So now, if I try to run the command:
from dfa import *
i get the error:
ImportError: No module named dfa
If i run the command:
os.path.isfile('dfa.pyc')
it returns true.
Can someone explain how i can get the dfa.pyc file imported?
Thanks
I don't think python modules are loaded I based on what you do with chdir. Modules are loaded from the folder you started the python shell and folders in PYTHONPATH.
If you want dynamically load modules maybe you can check imp.loadmodule (sample in the bottom of the page).
you can add to the PYTHONPATH in code by doing
sys.path.append('<newpath'>)
from dfa import *
I don't believe changing your current directory has any impact on the import process and even if it did, I'm not sure that's how you would want to do it.
from Brian Fitzgerald in Loading (and unloading) Python modules
"
... and this to “un-import”
del sys.modules["package"]
del package
"
I'm trying to use pywn, a python library for using WordNet. I've played about with python a little under Windows, but am completely new at MacOS X stuff. I'm running under MacOS 10.5.8, so my default Python interpreter is 2.5.1
The pywn instructions say: "Put each of the .py files somewhere in your python search path."
Where is the python search path defined under the default python installation in MacOS X?
If I've put the pywn files in /Users/nick/programming/pywn, what is the best way of adding this to the search path?
Is this the best place to put the files?
Try print sys.path from a Python shell. This will tell you what directories Python is searching for modules.
It is set via a combination of an environment variable (PYTHONPATH) and a base set of directories specific to your installation.
For more info: http://docs.python.org/library/sys.html#sys.path
More generally, if you do not have enough administrative rights to modify the system, or if you want to keep some modules in your home directory, you can either do:
import sys
sys.path.append('/Users/nick/programming/')
import pywn
You can alternatively add /Users/nick/programming to the environment variable PYTHONPATH, which has the advantage of giving you direct access to pywn through "import pywn". The default place on Mac OS X would be a .bashrc file in your home directory, which should then contain:
export PYTHONPATH="/Users/nick/programming/"
(separate multiple paths with ":", if necessary). You could then access pywn directly from any Python program with a simple "impot pywn".
I think by default /Library/Python/2.5/site-packages/ is part of your search path. This directory is usually used for third party libraries.
Simply use:
easy_install [LIBRARY NAME]
By the way... need to be root so before that:
su root