-1

I'm trying to create a file called fibo.py and import it into python (See here https://docs.python.org/3/tutorial/modules.html) I can't figure out where "my current directory" is in order to save fibo.py so that Python can find it.

Can someone please help me? I'm running Windows 10 and python 3.8

Fiona Kwok
  • 87
  • 2
  • 8
  • Are you running your python repl in the directory where ```fibo.py``` is saved? – irahorecka Jul 12 '20 at 01:58
  • How did you install Python? –  Jul 12 '20 at 02:03
  • 1
    Probably not exactly what you're looking for but Python looks into few directories to resolve the `import` path of a module/package. https://docs.python.org/3/tutorial/modules.html#the-module-search-path – shriek Jul 12 '20 at 02:30
  • Does this answer your question? [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – Joundill Jul 12 '20 at 23:02

5 Answers5

0

Run the Print Working Directory program to display your current directory. You can do this by typing "pwd" in your terminal/command prompt, and then pressing enter.

Austin O.
  • 1
  • 2
0

Ok I can give you a basic answer. I am on a Mac so I will give a Mac example, Windows will be a little different, but the principles will be similar. Say I am in my Desktop folder.

I create my fibo.py file in Desktop and in it is my nice python code. So fibo.py's location will look like: Desktop/fibo.py.

Now to retrieve fibo.py in my python repl, I will:

  1. Navigate to Desktop in my terminal
  2. Type python3 to open the python repl (note: I am in my Desktop directory)
  3. >>> import fibo
  4. >>> fibo.fib(1000)
irahorecka
  • 1,447
  • 8
  • 25
0

create the file in desktop and type the following lines in your cmd:

cd desktop py

import fibo.py

here is what you should see

Microsoft Windows [Version 10.0.19041.329] (c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\Mista>cd desktop

C:\Users\Mista\Desktop>py Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import fibo

Yappi
  • 43
  • 4
0

To get your current directory in python you can do this:

import os

print os.getcwd()   #<-- print your current working directory

if you are running you script from shell window, just navigate to the directory where you have the module you want to import first.

path = 'fibo_folder'    #<-- path of your fibo.py module

os.chdir(path)          #<-- navigate to that path

then you can import it:

import fibo

or you could also append that module to your python path:

import sys
path = 'my_module'
sys.path.append(path)

Or if you just want to have it ready available for import from any where you can just place your module inside your python installation root directory:

C:\Python27\

Or inside your python scripts folder:

C:\Python27\Scripts\ folder

Diego Suarez
  • 901
  • 13
  • 16
0

In the page you mentioned, python import fibo from installed package of python.

If you encounter the following error after import:

ModuleNotFoundError: No module named 'fibo'

You need to install the fibo package using the pip in command prompt:

python3 -m pip install fibo